r - 在 R ggplot 中控制矩形 geom_ribbon

标签 r ggplot2

我正在使用 ggplot,并试图将一个简单矩形形式的功能区添加到我拥有的条形图中。这个想法是显示低于某个值的截止值。

条形图很好,但我不能完全正确地使用功能区 - 我希望它显示得更宽一些,但它似乎仅限于条形图数据的宽度。

我尝试使用 xminxmax但这不会增加阴影区域的宽度。

有没有办法显式控制 geom_ribbon 的宽度?

# Where df is a data frame containing the data to plot
library(cowplot)

ggplot(df, aes(x=treatments, y=propNotEliminated)) +
  geom_ribbon(aes(xmin=0, xmax=21, ymin=0, ymax=20)) +  # the xmin and xmax don't do what I'd expect
  geom_bar(stat="identity", fill="white", colour="black", size=1) +
  theme_cowplot()

Resulting plot

最佳答案

为什么不使用 geom_rect ?

ggplot(mtcars, aes(factor(cyl))) + 
  geom_bar() +
  geom_rect(xmin = 0, xmax = Inf, ymin = 0, ymax = 1, fill = "blue") + 
  geom_rect(xmin = 1, xmax = 3, ymin = 1, ymax = 2, fill = "red") + 
  geom_rect(xmin = 1 - 0.5, xmax = 3 + 0.5, ymin = 2, ymax = 3, fill = "green") 

enter image description here

在您对展示位置感到满意后,输入 geom_bar最后的。

关于r - 在 R ggplot 中控制矩形 geom_ribbon,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31291067/

相关文章:

r - 不带组的data.table线性内插NA值

r - 将数据帧乘以向量的正确方法是什么?

r - read_html(url) 和 read_html(content(GET(url), "text")) 之间的区别

r - 从全局环境访问对象,这些对象直接传递给 ggplot 包装器中的函数

r - 在 ggplot2 中绘制线条和群体审美

不同长度的 rbind 向量 : pad with zero (or NA) instead of recycling

javascript - 默认情况下, Shiny 的应用程序数据对于特殊输入小部件不可见

r - 将数据框附加到 shapefile 并绘制它

r - 用于将 R 图添加到演示文稿的官员包函数

r - geom_bar 和 geom_linerange 之间的技术差异