【发布时间】:2021-12-17 23:27:03
【问题描述】:
我有一个关于应用 ggplot 的色阶渐变的问题。我有数据集,其中响应变量是包含正数和负数的连续变量,自变量是许多独立站点。我试图以这样一种方式绘制数据,即我可以在背景中绘制所有数据,然后将色阶渐变应用于覆盖数据负范围的响应数据。到目前为止,这就是我所拥有的示例数据集,该示例数据集模仿了我的实际数据集的结构。
tr_sim <- data.frame(site_id = seq(1,100,1), estimated_impact =
rnorm(100,18,200), impact_group = rep(c(1,2),each = 50))
rng_full <- range(tr_sim$estimated_impact)
#produce plot showing the full range of impacts across all sites and then
over the subsetted sites
impact_plot_full <- ggplot(data = tr_sim, aes(x = factor(site_id, levels =
site_id[order(estimated_impact)]), y = estimated_impact)) +
geom_bar(stat = "identity",width = 1, fill = "grey80")
impact_plot_full
impact_plot_full +
geom_bar(stat = "identity", width = 1, position = "stack", aes(y =
estimated_impact[impact_group == 1])) +
scale_fill_gradient2(low="firebrick", mid="yellow", high = "green4") +
labs(y = "Estimated Impact ($/week)", x = "Total number of sites with estimate
is 100", title = "Sites with the greatest impact after coverage loss") +
theme(axis.text.x = element_blank()) +
scale_y_continuous(breaks =
round(seq(rng_full[1],rng_full[2],by=100),digits=0))
我可以将背景中的所有数据绘制为灰色,并尝试在此之上绘制响应数据的负值范围。我得到的错误是“美学必须是长度 1 或与数据(100)、y、x 相同”。我知道这是因为负数据与整个数据集的长度不同,但我想不出办法做到这一点。任何建议将不胜感激。
谢谢你, 柯蒂斯
【问题讨论】:
标签: r ggplot2 color-scheme