我正在使用 ggplot,并试图将一个简单矩形形式的功能区添加到我拥有的条形图中。这个想法是显示低于某个值的截止值。
条形图很好,但我不能完全正确地使用功能区 - 我希望它显示得更宽一些,但它似乎仅限于条形图数据的宽度。
我尝试使用 xmin
和 xmax
但这不会增加阴影区域的宽度。
有没有办法显式控制 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()
最佳答案
为什么不使用 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")
在您对展示位置感到满意后,输入
geom_bar
最后的。
关于r - 在 R ggplot 中控制矩形 geom_ribbon,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31291067/