Grid.arrange背景

时间:2016-01-12 22:56:45

标签: r ggplot2 r-grid

我制作了一个类似下面代码所描述的情节,导致发布的图像。我无法弄清楚如何将整个背景设置为相同的" grey80" -color我在定义子图时使用过。 IE浏览器。我想以相同的颜色为图表之间和图例两侧的白色区域着色。 这是否有可能实现,或许有一些花哨的gridgrob-magic?

这个问题与change the background color of grid.arrange output类似,但我想要一个不使用png()函数的解决方案,如果可能的话

library(ggplot2)
library(gridExtra)
library(ggthemes)
library(grid)

p1 <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width,
                       colour = Species)) + 
  ggtitle('Sepal') + 
  geom_point() + theme_bw() + 
  theme(rect = element_rect(fill = 'grey80'))


p2 <- ggplot(iris, aes(x = Petal.Length, y = Petal.Width,
                       colour = Species)) + 
  ggtitle('Petal') + 
  geom_point() + theme_bw() + 
  theme(rect = element_rect(fill = 'grey80'))

grid_arrange_shared_legend <- function(...) {
  plots <- list(...)
  g <- ggplotGrob(plots[[1]] + theme(legend.position = "bottom"))$grobs
  legend <- g[[which(sapply(g, function(x) x$name) == "guide-box")]]
  lheight <- sum(legend$height)
  grid.arrange(
    do.call(arrangeGrob, lapply(plots, function(x)
      x + theme(legend.position="none"))),
    legend,
    ncol = 1,
    heights = unit.c(unit(1, "npc") - lheight, lheight))
}

grid_arrange_shared_legend(p1,p2)

Grid arrange plot

2 个答案:

答案 0 :(得分:3)

升级评论

您可以通过在图形窗口中添加灰色背景来实现此目的 然后在顶部添加图。由于您的图例功能使用grid.arrange,因此会生成新页面;所以要么添加newpage=FALSE,要么将arrangeGrob更改为您的函数。

你的例子

library(ggplot2)
library(gridExtra)
library(ggthemes)
library(grid)

p1 <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, colour = Species)) + 
         ggtitle('Sepal') + 
         geom_point() + theme_bw() + 
         # by adding colour=grey removes the white border of the plot and
         # so removes the lines between the plots
         # add panel.background = element_rect(fill = "grey80") 
         # if you want the plot panel grey aswell
         theme(plot.background=element_rect(fill="grey80", colour="grey80"),
               rect = element_rect(fill = 'grey80'))

p2 <- ggplot(iris, aes(x = Petal.Length, y = Petal.Width, colour = Species)) + 
         ggtitle('Petal') + 
         geom_point() + theme_bw() + 
         theme(plot.background=element_rect(fill="grey80", colour="grey80"),
               rect = element_rect(fill = 'grey80'))

Tweal your function

# Change grid.arrange to arrangeGrob
grid_arrange_shared_legend <- function(...) {
  plots <- list(...)
  g <- ggplotGrob(plots[[1]] + theme(legend.position = "bottom"))$grobs
  legend <- g[[which(sapply(g, function(x) x$name) == "guide-box")]]
  lheight <- sum(legend$height)
  arrangeGrob( # change here
    do.call(arrangeGrob, lapply(plots, function(x)
                                   x + theme(legend.position="none"))),
    legend,
    ncol = 1,
    heights = unit.c(unit(1, "npc") - lheight, lheight))
}

剧情

grid.draw(grobTree(rectGrob(gp=gpar(fill="grey80", lwd=0)), 
                   grid_arrange_shared_legend(p1,p2)))

哪个给出了

enter image description here

答案 1 :(得分:1)

我认为您可以利用here所示的ggdraw()包中的cowplot函数。

在您的情况下,您只需要在每个图plot.background = element_rect(fill="grey80", color = NA)theme()的{​​{1}}中添加p1,然后将它们与函数{{1}缝合在一起},然后在其输出上调用p2

grid_arrange_shared_legend()

enter image description here