在ggplot2中向plot背景添加一个geom_rect(不是面板) [英] Add a geom_rect to the plot background (not panel) in ggplot2
问题描述
我附上了538图的屏幕截图 -
I have attached a screenshot of a 538 plot - https://scontent-lax3-2.xx.fbcdn.net/v/t31.0-8/22218477_1588451034532053_5652546964446428775_o.png?oh=aa7436a960f2982deed7a90c997f31e4&oe=5A45A9E7 - and I am trying to format my ggplot in a similar manner. The part I am currently struggling with is the dark grey shading on the bottom. I have a demo plot here:
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) +
geom_point() +
theme(plot.background = element_rect(fill = 'gray95'),
panel.background = element_rect(fill = 'red')) +
labs(caption = 'mycaption')
panel.background colors the entire panel red, and plot.background colors everything to the outside of the panel grey. I am familiar with geom_rect(), however these can only be placed INSIDE the panel from what I've read, with parameters for xmin/xmax/ymin/ymax... Is there anyway for me to add a geom_rect() on the bottom of the ENTIRE GRAPH, and not simply the bottom of the panel between x and y coordinates?
Let me know if this makes sense at all, otherwise I can explain more.
Thanks!
there are already several answers on this site; here's one approach:
library(gridExtra)
fthbar <- grobTree(rectGrob(gp=gpar(fill="grey50",col=NA)),
textGrob("LEFT", x=0, hjust=0, gp=gpar(col="white")),
textGrob("RIGHT", x=1, hjust=1, gp=gpar(col="white")), cl="ann")
heightDetails.ann <- function(x) unit(1,"line")
grid.arrange(qplot(rnorm(10),rnorm(10)), bottom = fthbar)
这篇关于在ggplot2中向plot背景添加一个geom_rect(不是面板)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!