使用plot_grid更改背景颜色

时间:2018-11-19 08:27:43

标签: r ggplot2 r-grid cowplot

使用plot_grid时如何更改背景颜色?我有以下图形,但我希望背景中的所有内容均为灰色,并且不具有高度差异。我该如何更改?

enter image description here

这是我用于图形和数据的代码:

数据

   set.seed(123456)
    Test_1 <- round(rnorm(20,mean=35,sd=3),0)/100
    Test_2 <- round(rnorm(20,mean=70,sd=3),0)/100
    ei.data <- as.data.frame(cbind(Test_1,Test_2))

intercept <- as.data.frame(matrix(0,20,1))
slope <- as.data.frame(matrix(0,20,1))
data <- cbind(intercept,slope)
colnames(data) <- c("intercept","slope")
for (i in 1:nrow(ei.data)){
  data[i,1] <- (ei.data[i,2]/(1-ei.data[i,1]))
  data[i,2] <- ((ei.data[i,1]/(1-ei.data[i,1]))*(-1))
}

左图

p <- ggplot(data, aes(Test_1,Test_2))+
  geom_point(shape=1,size=1)+
  theme_bw()+
  xlab(TeX("$n_{1,i}$"))+
  ylab(TeX("$t_{1,i}$"))+
  scale_y_continuous(limits=c(0,1),breaks=seq(0,1,0.2))+
  scale_x_continuous(limits = c(0,1),breaks=seq(0,1,0.2))+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_rect(fill = "grey92", colour = NA),
        plot.background = element_rect(fill = "grey92", colour = NA),
        axis.line = element_line(colour = "black"))+
  theme(aspect.ratio=1)

p

右图

df <- data.frame()

q <- ggplot(df)+
  geom_point()+
  theme_bw()+
  scale_y_continuous(limits = c(0, 1),breaks=seq(0,1,0.2))+
  scale_x_continuous(limits = c(0, 1),breaks=seq(0,1,0.2))+
  xlab(TeX("$\\beta_i^{1,1}"))+
  ylab(TeX("$\\beta_i^{2,1}"))+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_rect(fill = "grey92", colour = NA),
        plot.background = element_rect(fill = "grey92", colour = NA), axis.line = element_line(colour = "black"))+
  theme(aspect.ratio=1)+
  geom_abline(slope =data[1,2] , intercept =data[1,1], size = 0.3)+
  geom_abline(slope =data[2,2] , intercept =data[2,1], size = 0.3)+
  geom_abline(slope =data[3,2] , intercept =data[3,1], size = 0.3)+
  geom_abline(slope =data[4,2] , intercept =data[4,1], size = 0.3)+
  geom_abline(slope =data[5,2] , intercept =data[5,1], size = 0.3)+
  geom_abline(slope =data[6,2] , intercept =data[6,1], size = 0.3)+
  geom_abline(slope =data[7,2] , intercept =data[7,1], size = 0.3)+
  geom_abline(slope =data[8,2] , intercept =data[8,1], size = 0.3)+
  geom_abline(slope =data[9,2] , intercept =data[9,1], size = 0.3)+
  geom_abline(slope =data[10,2] , intercept =data[10,1], size = 0.3)+
  geom_abline(slope =data[11,2] , intercept =data[11,1], size = 0.3)+
  geom_abline(slope =data[12,2] , intercept =data[12,1], size = 0.3)+
  geom_abline(slope =data[13,2] , intercept =data[13,1], size = 0.3)+
  geom_abline(slope =data[14,2] , intercept =data[14,1], size = 0.3)+
  geom_abline(slope =data[15,2] , intercept =data[15,1], size = 0.3)+
  geom_abline(slope =data[16,2] , intercept =data[16,1], size = 0.3)+
  geom_abline(slope =data[17,2] , intercept =data[17,1], size = 0.3)+
  geom_abline(slope =data[18,2] , intercept =data[18,1], size = 0.3)+
  geom_abline(slope =data[19,2] , intercept =data[19,1], size = 0.3)+
  geom_abline(slope =data[20,2] , intercept =data[20,1], size = 0.3)
q

安排

plot_grid(p,q,ncol=2, align = "v")

3 个答案:

答案 0 :(得分:5)

由于您以相同的方式自定义图表,因此,让我们可以更轻松地调整这些自定义(如果您改变主意):

theme_plt <- function() {
  theme_bw() + 
    theme(
      panel.grid.major = element_blank(), 
      panel.grid.minor = element_blank(),
      panel.background = element_rect(fill = "grey92", colour = NA),
      plot.background = element_rect(fill = "grey92", colour = NA),
      axis.line = element_line(colour = "black")
    ) +
    theme(aspect.ratio = 1)
}

common_scales <- function() {
  list(
    scale_y_continuous(limits = c(0, 1), breaks = seq(0, 1, 0.2)),
    scale_x_continuous(limits = c(0, 1), breaks = seq(0, 1, 0.2))
  )
}

您的左侧绘图调用对data使用了错误的参数,该参数已在此处修复:

ggplot(ei.data, aes(Test_1, Test_2)) +
  geom_point(shape = 1, size = 1) +
  common_scales() +
  labs(
    x = TeX("$n_{1,i}$"), y = TeX("$t_{1,i}$")
  ) +
  theme_plt() -> gg1

您可以通过以下方式简化abline的重复性:

ggplot() +
  geom_point() +
  geom_abline(
    data = data, aes(slope = slope, intercept = intercept), size = 0.3
  ) +
  common_scales() +
  labs(
    x = TeX("$\\beta_i^{1,1}"), y = TeX("$\\beta_i^{2,1}")
  ) +
  theme_plt() -> gg2

现在,高度差异的原因是由于右图同时具有子和超级重复脚本。因此,我们可以通过以下方式确保所有位的高度相同(因为这些图共有相同的图面积元素):

gt1 <- ggplot_gtable(ggplot_build(gg1))
gt2 <- ggplot_gtable(ggplot_build(gg2))

gt1$heights <- gt2$heights

让我们看看:

cowplot::plot_grid(gt1, gt2, ncol = 2, align = "v")

enter image description here

您无法从^^分辨出来,但是由于设置了aspect.ratio,因此在图表的上方和下方都有水平的白色边距/边框。 RStudio永远不会用白色以外的任何其他颜色来显示这种颜色(在1.2中,“暗”模式下的元素可能最终是“黑色”)。

其他绘图设备具有bg颜色,可以指定。我们可以使用magick设备并设置适当的高度/宽度以确保没有白色边框/空白:

image_graph(900, 446, bg = "grey92")
cowplot::plot_grid(gt1, gt2, ncol = 2, align = "v")
dev.off()
如果绘图窗格/窗口的尺寸未设置为高宽比,但实际绘图“图像”将不包含任何内容,则

^^仍会看起来像 ,在RStudio中具有顶部/底部边框。

答案 1 :(得分:3)

使用png(),您可以通过更改bg来正确保存图像:

png(bg = "grey92") # set the same bg
cowplot::plot_grid(p,q,ncol=2, align = "v")
#gridExtra::grid.arrange(p,q,ncol=2)
dev.off()

enter image description here

更新

这样,您甚至可以删除图形中的白色边框(无需保存png):

library(gridExtra)
library(grid)
grid.draw(grobTree(rectGrob(gp=gpar(fill="grey92", lwd=0)), # this changes the bg in the graphics (R viewer)
                   arrangeGrob(p,q,ncol=2)))

enter image description here

答案 2 :(得分:0)

我认为提供的各种解决方案过于复杂。由于cowplot::plot_grid()返回了一个新的ggplot2对象,因此您可以简单地使用ggplot2的主题化机制设置样式。

首先是问题代码的可再现示例,如简化的here:

library(ggplot2)
library(latex2exp)

set.seed(123456)
Test_1 <- round(rnorm(20,mean=35,sd=3),0)/100
Test_2 <- round(rnorm(20,mean=70,sd=3),0)/100
ei.data <- as.data.frame(cbind(Test_1,Test_2))

intercept <- as.data.frame(matrix(0,20,1))
slope <- as.data.frame(matrix(0,20,1))
data <- cbind(intercept,slope)
colnames(data) <- c("intercept","slope")
for (i in 1:nrow(ei.data)){
  data[i,1] <- (ei.data[i,2]/(1-ei.data[i,1]))
  data[i,2] <- ((ei.data[i,1]/(1-ei.data[i,1]))*(-1))
}

theme_plt <- function() {
  theme_bw() + 
    theme(
      panel.grid.major = element_blank(), 
      panel.grid.minor = element_blank(),
      panel.background = element_rect(fill = "grey92", colour = NA),
      plot.background = element_rect(fill = "grey92", colour = NA),
      axis.line = element_line(colour = "black")
    ) +
    theme(aspect.ratio = 1)
}

common_scales <- function() {
  list(
    scale_y_continuous(limits = c(0, 1), breaks = seq(0, 1, 0.2)),
    scale_x_continuous(limits = c(0, 1), breaks = seq(0, 1, 0.2))
  )
}

ggplot(ei.data, aes(Test_1, Test_2)) +
  geom_point(shape = 1, size = 1) +
  common_scales() +
  labs(
    x = TeX("$n_{1,i}$"), y = TeX("$t_{1,i}$")
  ) +
  theme_plt() -> gg1

ggplot() +
  geom_point() +
  geom_abline(
    data = data, aes(slope = slope, intercept = intercept), size = 0.3
  ) +
  common_scales() +
  labs(
    x = TeX("$\\beta_i^{1,1}"), y = TeX("$\\beta_i^{2,1}")
  ) +
  theme_plt() -> gg2

cowplot::plot_grid(gg1, gg2, align = "v")

我们可以看到,两个图形的尺寸略有不同,因此背景不匹配。

解决方案是在plot_grid()调用之后简单地添加一个主题声明:

cowplot::plot_grid(gg1, gg2, align = "v") +
  theme(plot.background = element_rect(fill = "grey92", colour = NA))

这将创建所选颜色的统一背景。当然,您必须调整图的输出尺寸,以避免在两个图的上方和下方出现大量的灰色。

为更清楚地突出显示正在发生的事情,让我们用不同的颜色选择来设置组合图的样式:

cowplot::plot_grid(gg1, gg2, align = "v") +
  theme(plot.background = element_rect(fill = "cornsilk", colour = "blue"))

我们可以看到主题声明已应用到plot_grid()粘贴了两个图的画布上。

最后,我们可以问为什么问题首先存在,而答案是因为曲线未对齐。为了使它们完美对齐,我们需要在水平和垂直方向进行对齐,并且在进行此操作时按预期进行:

cowplot::plot_grid(gg1, gg2, align = "vh")

通常,align = "h"就足够了(当将图放置在同一行中时,align = "v"是不正确的),但是由于主题的纵横比是固定的,因此我们需要水平和垂直对齐,因此align = "vh"