![](/img/trans.png)
[英]Drawing elements (arrows & circle) in ggplot (R) to show the difference between two bars
[英]Insert a grid between two elements with ggplot in R
我正在嘗試使用 geom_rect 覆蓋我的 plot 的背景,顏色取決於 x 軸。 例如,對於介於 0.5 和 1.5 之間的 x 值,背景將為灰色。 對於 1.5 和 2.5 之間的 x 值,它將是白色,然后是灰色等。我的 plot 上還有一個網格和箱線圖。
我希望網格位於此彩色背景之上,但位於箱線圖之后。
p <- (p
+ theme_bw()
+ ggtitle(dates[i])
+ xlab("Echeance")
+ geom_rect(xmin=0.5, xmax=1.5, ymin=500, ymax=7500, fill="gray90")
+ geom_rect(xmin=1.5, xmax=2.5, ymin=500, ymax=7500, fill="gray100")
+ geom_rect(xmin=2.5, xmax=3.5, ymin=500, ymax=7500, fill="gray90")
+ geom_line(data=data, aes(x=Var3, y=value), group=1)
+ geom_boxplot(aes(linetype=exp))
+ guides(linetype="none")
+ scale_y_continuous(n.breaks = 20))
沒有選項,我在網格頂部和箱線圖后面有彩色背景。
如果我添加
+ theme(panel.ontop = TRUE, panel.background = element_rect(fill = NA))
然后我將網格放在彩色背景之上,但也在箱線圖之上......
有人有訣竅嗎?
使用(而不是 geom_rect)
+ annotate("rect", xmin=2.5, xmax=3.5, ymin = -Inf, ymax = Inf, fill="gray20", alpha = 0.2)
用 alpha 控制透明度
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.