Search code examples
rggplot2r-gridcowplot

Change background color with plot_grid


how can I change the background color, when using plot_grid? I have the following graphic, but I want everything in the background to be grey and not have the difference in heights. How can I change this?

enter image description here

Here is my code for the graphics and the data:

Data

   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))
}

Left Plot

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

Right Plot

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

Arranging

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

Solution

  • Since you customize the plots the same way, let's make it easier to tweak those customizations (in the event you change your mind):

    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))
      )
    }
    

    Your left plot call uses the wrong parameter to data which is fixed here:

    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
    

    You can simplify your abline repetitiveness via:

    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
    

    Now, the reason for the height diffs is due to the right plot having both sub-, and super-duper scripts. So, we can ensure all the bits are the same height (since these plots have the same plot area elements in common) via:

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

    Let's take a look:

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

    enter image description here

    You can't tell from ^^ but there's a horizontal white margin/border above & below the graphs due to the aspect.ratio you've set. RStudio is never going to show that in any other color but white (mebbe, possibly "black" in "dark" mode in 1.2 eventually).

    Other plot devices have a bg color which you can specify. We can use the magick device and put in proper height/width to ensure no white borders/margin:

    image_graph(900, 446, bg = "grey92")
    cowplot::plot_grid(gt1, gt2, ncol = 2, align = "v")
    dev.off()
    

    ^^ will still look like it has a top/bottom border in RStudio if the plot pane/window is not sized to the aspect ratio but the actual plot "image" will not have any.