繁体   English   中英

r - 如何在 ggplot2 中增加面板背景的大小

[英]r - How can I increase the size of the panel background in ggplot2

我想增加 ggplot2 中面板背景的大小,以便扩展到轴标题上,如下图所示:

GDP图表

我尝试使用 element_rect 但没有成功。 这是我到目前为止所得到的。

dat <- data.frame(x=2001:2017, y=rnorm(17,100))
ggplot2::ggplot(data=dat, ggplot2::aes(x=x, y=y)) +
ggplot2::geom_line(size=1.5)  + 
ggplot2::theme(panel.background=ggplot2::element_rect(fill="darkgrey"), 
plot.background=ggplot2::element_rect(fill="lightgrey", color=NA)) + 
ggplot2::ggtitle("Title of the plot") 

我的版本

任何帮助,将不胜感激!

谢谢

丹尼尔

你可以试试

# first plot
p <- ggplot(data=dat, aes(x=x, y=y)) +
           geom_line(size=1.5)  + 
           theme(panel.background=element_rect(fill="darkgrey"),
                 plot.background=element_rect(fill="darkgrey"))
# blank plot
p1 <- ggplot(mtcars, aes(x = wt, y = mpg)) + 
      geom_blank() + 
      theme(plot.background=element_rect(fill="lightgrey"),
            line = element_blank(), 
            axis.text =  element_blank(),
            axis.title = element_blank(),
            plot.title = element_text(hjust = 0))

# plot with title
p2 <- p1 + ggtitle("Title of the plot", subtitle = "this is a sub") 
# plot with figure text
p3 <- p1 + ggtitle("",subtitle = "this is a second sub") 

# final plot
library(cowplot)
plot_grid(p2, p, p3,  nrow = 3, rel_heights = c(10, 80, 10))

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM