在 R ggplot 中控制矩形 geom_ribbon

Gis*_*wok 4 r ggplot2

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

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

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

有没有办法明确控制 的宽度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()
Run Code Online (Sandbox Code Playgroud)

结果图

ton*_*nov 6

为什么不使用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") 
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

在您对展示位置感到满意后,将其放在geom_bar最后。