Change background color with plot_grid












2















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









share|improve this question




















  • 2





    Didn't go through your code in detail, but pretty sure those multiple lines of geom_abline could be rewritten in a better/shorter way.

    – zx8754
    Nov 19 '18 at 9:24






  • 1





    indeed geom_abline(data = data, aes(slope = slope, intercept = intercept), size=0.3)

    – hrbrmstr
    Nov 19 '18 at 9:31











  • You're not going to get a different "background" color in the RStudio graphics panes if that's what you're after.

    – hrbrmstr
    Nov 19 '18 at 9:41











  • @hrbrmstr Thank you! That was very helpful.

    – Mucteam
    Nov 19 '18 at 10:05











  • Can you unaccept my answer? I need to delete it since — apparently — teaching folks the underpinnings of grid graphics as used in ggplot2 is no longer something I'm supposed to do on SO.

    – hrbrmstr
    Nov 25 '18 at 22:43


















2















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









share|improve this question




















  • 2





    Didn't go through your code in detail, but pretty sure those multiple lines of geom_abline could be rewritten in a better/shorter way.

    – zx8754
    Nov 19 '18 at 9:24






  • 1





    indeed geom_abline(data = data, aes(slope = slope, intercept = intercept), size=0.3)

    – hrbrmstr
    Nov 19 '18 at 9:31











  • You're not going to get a different "background" color in the RStudio graphics panes if that's what you're after.

    – hrbrmstr
    Nov 19 '18 at 9:41











  • @hrbrmstr Thank you! That was very helpful.

    – Mucteam
    Nov 19 '18 at 10:05











  • Can you unaccept my answer? I need to delete it since — apparently — teaching folks the underpinnings of grid graphics as used in ggplot2 is no longer something I'm supposed to do on SO.

    – hrbrmstr
    Nov 25 '18 at 22:43
















2












2








2








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









share|improve this question
















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






r ggplot2 grid cowplot






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 9:02









markus

12.4k1234




12.4k1234










asked Nov 19 '18 at 8:27









MucteamMucteam

857




857








  • 2





    Didn't go through your code in detail, but pretty sure those multiple lines of geom_abline could be rewritten in a better/shorter way.

    – zx8754
    Nov 19 '18 at 9:24






  • 1





    indeed geom_abline(data = data, aes(slope = slope, intercept = intercept), size=0.3)

    – hrbrmstr
    Nov 19 '18 at 9:31











  • You're not going to get a different "background" color in the RStudio graphics panes if that's what you're after.

    – hrbrmstr
    Nov 19 '18 at 9:41











  • @hrbrmstr Thank you! That was very helpful.

    – Mucteam
    Nov 19 '18 at 10:05











  • Can you unaccept my answer? I need to delete it since — apparently — teaching folks the underpinnings of grid graphics as used in ggplot2 is no longer something I'm supposed to do on SO.

    – hrbrmstr
    Nov 25 '18 at 22:43
















  • 2





    Didn't go through your code in detail, but pretty sure those multiple lines of geom_abline could be rewritten in a better/shorter way.

    – zx8754
    Nov 19 '18 at 9:24






  • 1





    indeed geom_abline(data = data, aes(slope = slope, intercept = intercept), size=0.3)

    – hrbrmstr
    Nov 19 '18 at 9:31











  • You're not going to get a different "background" color in the RStudio graphics panes if that's what you're after.

    – hrbrmstr
    Nov 19 '18 at 9:41











  • @hrbrmstr Thank you! That was very helpful.

    – Mucteam
    Nov 19 '18 at 10:05











  • Can you unaccept my answer? I need to delete it since — apparently — teaching folks the underpinnings of grid graphics as used in ggplot2 is no longer something I'm supposed to do on SO.

    – hrbrmstr
    Nov 25 '18 at 22:43










2




2





Didn't go through your code in detail, but pretty sure those multiple lines of geom_abline could be rewritten in a better/shorter way.

– zx8754
Nov 19 '18 at 9:24





Didn't go through your code in detail, but pretty sure those multiple lines of geom_abline could be rewritten in a better/shorter way.

– zx8754
Nov 19 '18 at 9:24




1




1





indeed geom_abline(data = data, aes(slope = slope, intercept = intercept), size=0.3)

– hrbrmstr
Nov 19 '18 at 9:31





indeed geom_abline(data = data, aes(slope = slope, intercept = intercept), size=0.3)

– hrbrmstr
Nov 19 '18 at 9:31













You're not going to get a different "background" color in the RStudio graphics panes if that's what you're after.

– hrbrmstr
Nov 19 '18 at 9:41





You're not going to get a different "background" color in the RStudio graphics panes if that's what you're after.

– hrbrmstr
Nov 19 '18 at 9:41













@hrbrmstr Thank you! That was very helpful.

– Mucteam
Nov 19 '18 at 10:05





@hrbrmstr Thank you! That was very helpful.

– Mucteam
Nov 19 '18 at 10:05













Can you unaccept my answer? I need to delete it since — apparently — teaching folks the underpinnings of grid graphics as used in ggplot2 is no longer something I'm supposed to do on SO.

– hrbrmstr
Nov 25 '18 at 22:43







Can you unaccept my answer? I need to delete it since — apparently — teaching folks the underpinnings of grid graphics as used in ggplot2 is no longer something I'm supposed to do on SO.

– hrbrmstr
Nov 25 '18 at 22:43














3 Answers
3






active

oldest

votes


















5














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.






share|improve this answer


























  • Perfect! Thank you so much! I realize, that there is still a lot I need to learn!

    – Mucteam
    Nov 19 '18 at 10:34











  • This is all very nice, but I’d like to point out that plot_grid() returns a ggplot2 object, so you can style it simply by adding a theme: plot_grid() + theme(plot.background = element_rect(fill = “grey92”)).

    – Claus Wilke
    Nov 23 '18 at 15:59













  • I'm not sure you understand what the OP was talking about

    – hrbrmstr
    Nov 23 '18 at 16:34











  • @hrbrmstr Respectfully, I think I do. stackoverflow.com/a/53472179/4975218

    – Claus Wilke
    Nov 25 '18 at 21:29











  • respectfully, they're not aligned in your example. i "get" what you're saying, but my solution is no less effective than yours. and both (yours or image_graph) aren't needed if the output is to, say, png() where you can specify the background color

    – hrbrmstr
    Nov 25 '18 at 21:33





















3














With png() you can correctly save the image by changing 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



UPDATE:



With this you can remove even the white border in the graphics (no need to save the 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






share|improve this answer


























  • The expression-laden plot labels are important since they're causing the height diff in the OP's actual plots. The package you need to load is latex2exp. This doesn't solve the height problem.

    – hrbrmstr
    Nov 19 '18 at 9:47











  • "but I want everything in the background to be grey", I showed two ways to solve the first problem stated by op, so I posted the answer. Thanks to point that out anyway.

    – RLave
    Nov 19 '18 at 9:50













  • @RLave Thank you very much. Thats exactly what I was looking for.

    – Mucteam
    Nov 19 '18 at 10:06



















0














I think the various solutions provided are overly complicated. Because cowplot::plot_grid() returns a new ggplot2 object, you can simply style that using ggplot2's themeing mechanisms.



First a reproducible example of the problem code, as simplified 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")




As we can see, the two figures have slightly different dimensions, and hence the background doesn't match.



The solution is to simply add a theme statement after the plot_grid() call:



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




This has created a uniform background of the chosen color. You would of course have to adjust the output dimensions of the plot to avoid the large amount of grey color above and below the two figures.



To highlight more clearly what's happening, let's style the combined plot with a different color choice:





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




We can see that the theme statement is applied to the canvas onto which the two plots are pasted by plot_grid().



Finally, we can ask why the problem exists in the first place, and the answer is because the plots are not aligned. To make them align perfectly, we need to align both vertically and horizontally, and when we do so things work as expected:



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


Normally, align = "h" would be sufficient (align = "v" is incorrect when the plots are placed in the same row), but because the theme has a fixed aspect ratio we need to align both horizontally and vertically, hence align = "vh".








share|improve this answer


























  • as noted in my comment to your comment, they aren't aligned

    – hrbrmstr
    Nov 25 '18 at 21:35











  • @hrbrmstr You're correct about the alignment. The plot_grid() call used the wrong align argument. I've addressed this now.

    – Claus Wilke
    Nov 25 '18 at 22:00











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53370824%2fchange-background-color-with-plot-grid%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









5














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.






share|improve this answer


























  • Perfect! Thank you so much! I realize, that there is still a lot I need to learn!

    – Mucteam
    Nov 19 '18 at 10:34











  • This is all very nice, but I’d like to point out that plot_grid() returns a ggplot2 object, so you can style it simply by adding a theme: plot_grid() + theme(plot.background = element_rect(fill = “grey92”)).

    – Claus Wilke
    Nov 23 '18 at 15:59













  • I'm not sure you understand what the OP was talking about

    – hrbrmstr
    Nov 23 '18 at 16:34











  • @hrbrmstr Respectfully, I think I do. stackoverflow.com/a/53472179/4975218

    – Claus Wilke
    Nov 25 '18 at 21:29











  • respectfully, they're not aligned in your example. i "get" what you're saying, but my solution is no less effective than yours. and both (yours or image_graph) aren't needed if the output is to, say, png() where you can specify the background color

    – hrbrmstr
    Nov 25 '18 at 21:33


















5














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.






share|improve this answer


























  • Perfect! Thank you so much! I realize, that there is still a lot I need to learn!

    – Mucteam
    Nov 19 '18 at 10:34











  • This is all very nice, but I’d like to point out that plot_grid() returns a ggplot2 object, so you can style it simply by adding a theme: plot_grid() + theme(plot.background = element_rect(fill = “grey92”)).

    – Claus Wilke
    Nov 23 '18 at 15:59













  • I'm not sure you understand what the OP was talking about

    – hrbrmstr
    Nov 23 '18 at 16:34











  • @hrbrmstr Respectfully, I think I do. stackoverflow.com/a/53472179/4975218

    – Claus Wilke
    Nov 25 '18 at 21:29











  • respectfully, they're not aligned in your example. i "get" what you're saying, but my solution is no less effective than yours. and both (yours or image_graph) aren't needed if the output is to, say, png() where you can specify the background color

    – hrbrmstr
    Nov 25 '18 at 21:33
















5












5








5







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.






share|improve this answer















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.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 19 '18 at 10:37

























answered Nov 19 '18 at 10:08









hrbrmstrhrbrmstr

60.9k688150




60.9k688150













  • Perfect! Thank you so much! I realize, that there is still a lot I need to learn!

    – Mucteam
    Nov 19 '18 at 10:34











  • This is all very nice, but I’d like to point out that plot_grid() returns a ggplot2 object, so you can style it simply by adding a theme: plot_grid() + theme(plot.background = element_rect(fill = “grey92”)).

    – Claus Wilke
    Nov 23 '18 at 15:59













  • I'm not sure you understand what the OP was talking about

    – hrbrmstr
    Nov 23 '18 at 16:34











  • @hrbrmstr Respectfully, I think I do. stackoverflow.com/a/53472179/4975218

    – Claus Wilke
    Nov 25 '18 at 21:29











  • respectfully, they're not aligned in your example. i "get" what you're saying, but my solution is no less effective than yours. and both (yours or image_graph) aren't needed if the output is to, say, png() where you can specify the background color

    – hrbrmstr
    Nov 25 '18 at 21:33





















  • Perfect! Thank you so much! I realize, that there is still a lot I need to learn!

    – Mucteam
    Nov 19 '18 at 10:34











  • This is all very nice, but I’d like to point out that plot_grid() returns a ggplot2 object, so you can style it simply by adding a theme: plot_grid() + theme(plot.background = element_rect(fill = “grey92”)).

    – Claus Wilke
    Nov 23 '18 at 15:59













  • I'm not sure you understand what the OP was talking about

    – hrbrmstr
    Nov 23 '18 at 16:34











  • @hrbrmstr Respectfully, I think I do. stackoverflow.com/a/53472179/4975218

    – Claus Wilke
    Nov 25 '18 at 21:29











  • respectfully, they're not aligned in your example. i "get" what you're saying, but my solution is no less effective than yours. and both (yours or image_graph) aren't needed if the output is to, say, png() where you can specify the background color

    – hrbrmstr
    Nov 25 '18 at 21:33



















Perfect! Thank you so much! I realize, that there is still a lot I need to learn!

– Mucteam
Nov 19 '18 at 10:34





Perfect! Thank you so much! I realize, that there is still a lot I need to learn!

– Mucteam
Nov 19 '18 at 10:34













This is all very nice, but I’d like to point out that plot_grid() returns a ggplot2 object, so you can style it simply by adding a theme: plot_grid() + theme(plot.background = element_rect(fill = “grey92”)).

– Claus Wilke
Nov 23 '18 at 15:59







This is all very nice, but I’d like to point out that plot_grid() returns a ggplot2 object, so you can style it simply by adding a theme: plot_grid() + theme(plot.background = element_rect(fill = “grey92”)).

– Claus Wilke
Nov 23 '18 at 15:59















I'm not sure you understand what the OP was talking about

– hrbrmstr
Nov 23 '18 at 16:34





I'm not sure you understand what the OP was talking about

– hrbrmstr
Nov 23 '18 at 16:34













@hrbrmstr Respectfully, I think I do. stackoverflow.com/a/53472179/4975218

– Claus Wilke
Nov 25 '18 at 21:29





@hrbrmstr Respectfully, I think I do. stackoverflow.com/a/53472179/4975218

– Claus Wilke
Nov 25 '18 at 21:29













respectfully, they're not aligned in your example. i "get" what you're saying, but my solution is no less effective than yours. and both (yours or image_graph) aren't needed if the output is to, say, png() where you can specify the background color

– hrbrmstr
Nov 25 '18 at 21:33







respectfully, they're not aligned in your example. i "get" what you're saying, but my solution is no less effective than yours. and both (yours or image_graph) aren't needed if the output is to, say, png() where you can specify the background color

– hrbrmstr
Nov 25 '18 at 21:33















3














With png() you can correctly save the image by changing 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



UPDATE:



With this you can remove even the white border in the graphics (no need to save the 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






share|improve this answer


























  • The expression-laden plot labels are important since they're causing the height diff in the OP's actual plots. The package you need to load is latex2exp. This doesn't solve the height problem.

    – hrbrmstr
    Nov 19 '18 at 9:47











  • "but I want everything in the background to be grey", I showed two ways to solve the first problem stated by op, so I posted the answer. Thanks to point that out anyway.

    – RLave
    Nov 19 '18 at 9:50













  • @RLave Thank you very much. Thats exactly what I was looking for.

    – Mucteam
    Nov 19 '18 at 10:06
















3














With png() you can correctly save the image by changing 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



UPDATE:



With this you can remove even the white border in the graphics (no need to save the 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






share|improve this answer


























  • The expression-laden plot labels are important since they're causing the height diff in the OP's actual plots. The package you need to load is latex2exp. This doesn't solve the height problem.

    – hrbrmstr
    Nov 19 '18 at 9:47











  • "but I want everything in the background to be grey", I showed two ways to solve the first problem stated by op, so I posted the answer. Thanks to point that out anyway.

    – RLave
    Nov 19 '18 at 9:50













  • @RLave Thank you very much. Thats exactly what I was looking for.

    – Mucteam
    Nov 19 '18 at 10:06














3












3








3







With png() you can correctly save the image by changing 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



UPDATE:



With this you can remove even the white border in the graphics (no need to save the 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






share|improve this answer















With png() you can correctly save the image by changing 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



UPDATE:



With this you can remove even the white border in the graphics (no need to save the 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







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 19 '18 at 9:51

























answered Nov 19 '18 at 9:15









RLaveRLave

4,42711023




4,42711023













  • The expression-laden plot labels are important since they're causing the height diff in the OP's actual plots. The package you need to load is latex2exp. This doesn't solve the height problem.

    – hrbrmstr
    Nov 19 '18 at 9:47











  • "but I want everything in the background to be grey", I showed two ways to solve the first problem stated by op, so I posted the answer. Thanks to point that out anyway.

    – RLave
    Nov 19 '18 at 9:50













  • @RLave Thank you very much. Thats exactly what I was looking for.

    – Mucteam
    Nov 19 '18 at 10:06



















  • The expression-laden plot labels are important since they're causing the height diff in the OP's actual plots. The package you need to load is latex2exp. This doesn't solve the height problem.

    – hrbrmstr
    Nov 19 '18 at 9:47











  • "but I want everything in the background to be grey", I showed two ways to solve the first problem stated by op, so I posted the answer. Thanks to point that out anyway.

    – RLave
    Nov 19 '18 at 9:50













  • @RLave Thank you very much. Thats exactly what I was looking for.

    – Mucteam
    Nov 19 '18 at 10:06

















The expression-laden plot labels are important since they're causing the height diff in the OP's actual plots. The package you need to load is latex2exp. This doesn't solve the height problem.

– hrbrmstr
Nov 19 '18 at 9:47





The expression-laden plot labels are important since they're causing the height diff in the OP's actual plots. The package you need to load is latex2exp. This doesn't solve the height problem.

– hrbrmstr
Nov 19 '18 at 9:47













"but I want everything in the background to be grey", I showed two ways to solve the first problem stated by op, so I posted the answer. Thanks to point that out anyway.

– RLave
Nov 19 '18 at 9:50







"but I want everything in the background to be grey", I showed two ways to solve the first problem stated by op, so I posted the answer. Thanks to point that out anyway.

– RLave
Nov 19 '18 at 9:50















@RLave Thank you very much. Thats exactly what I was looking for.

– Mucteam
Nov 19 '18 at 10:06





@RLave Thank you very much. Thats exactly what I was looking for.

– Mucteam
Nov 19 '18 at 10:06











0














I think the various solutions provided are overly complicated. Because cowplot::plot_grid() returns a new ggplot2 object, you can simply style that using ggplot2's themeing mechanisms.



First a reproducible example of the problem code, as simplified 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")




As we can see, the two figures have slightly different dimensions, and hence the background doesn't match.



The solution is to simply add a theme statement after the plot_grid() call:



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




This has created a uniform background of the chosen color. You would of course have to adjust the output dimensions of the plot to avoid the large amount of grey color above and below the two figures.



To highlight more clearly what's happening, let's style the combined plot with a different color choice:





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




We can see that the theme statement is applied to the canvas onto which the two plots are pasted by plot_grid().



Finally, we can ask why the problem exists in the first place, and the answer is because the plots are not aligned. To make them align perfectly, we need to align both vertically and horizontally, and when we do so things work as expected:



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


Normally, align = "h" would be sufficient (align = "v" is incorrect when the plots are placed in the same row), but because the theme has a fixed aspect ratio we need to align both horizontally and vertically, hence align = "vh".








share|improve this answer


























  • as noted in my comment to your comment, they aren't aligned

    – hrbrmstr
    Nov 25 '18 at 21:35











  • @hrbrmstr You're correct about the alignment. The plot_grid() call used the wrong align argument. I've addressed this now.

    – Claus Wilke
    Nov 25 '18 at 22:00
















0














I think the various solutions provided are overly complicated. Because cowplot::plot_grid() returns a new ggplot2 object, you can simply style that using ggplot2's themeing mechanisms.



First a reproducible example of the problem code, as simplified 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")




As we can see, the two figures have slightly different dimensions, and hence the background doesn't match.



The solution is to simply add a theme statement after the plot_grid() call:



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




This has created a uniform background of the chosen color. You would of course have to adjust the output dimensions of the plot to avoid the large amount of grey color above and below the two figures.



To highlight more clearly what's happening, let's style the combined plot with a different color choice:





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




We can see that the theme statement is applied to the canvas onto which the two plots are pasted by plot_grid().



Finally, we can ask why the problem exists in the first place, and the answer is because the plots are not aligned. To make them align perfectly, we need to align both vertically and horizontally, and when we do so things work as expected:



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


Normally, align = "h" would be sufficient (align = "v" is incorrect when the plots are placed in the same row), but because the theme has a fixed aspect ratio we need to align both horizontally and vertically, hence align = "vh".








share|improve this answer


























  • as noted in my comment to your comment, they aren't aligned

    – hrbrmstr
    Nov 25 '18 at 21:35











  • @hrbrmstr You're correct about the alignment. The plot_grid() call used the wrong align argument. I've addressed this now.

    – Claus Wilke
    Nov 25 '18 at 22:00














0












0








0







I think the various solutions provided are overly complicated. Because cowplot::plot_grid() returns a new ggplot2 object, you can simply style that using ggplot2's themeing mechanisms.



First a reproducible example of the problem code, as simplified 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")




As we can see, the two figures have slightly different dimensions, and hence the background doesn't match.



The solution is to simply add a theme statement after the plot_grid() call:



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




This has created a uniform background of the chosen color. You would of course have to adjust the output dimensions of the plot to avoid the large amount of grey color above and below the two figures.



To highlight more clearly what's happening, let's style the combined plot with a different color choice:





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




We can see that the theme statement is applied to the canvas onto which the two plots are pasted by plot_grid().



Finally, we can ask why the problem exists in the first place, and the answer is because the plots are not aligned. To make them align perfectly, we need to align both vertically and horizontally, and when we do so things work as expected:



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


Normally, align = "h" would be sufficient (align = "v" is incorrect when the plots are placed in the same row), but because the theme has a fixed aspect ratio we need to align both horizontally and vertically, hence align = "vh".








share|improve this answer















I think the various solutions provided are overly complicated. Because cowplot::plot_grid() returns a new ggplot2 object, you can simply style that using ggplot2's themeing mechanisms.



First a reproducible example of the problem code, as simplified 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")




As we can see, the two figures have slightly different dimensions, and hence the background doesn't match.



The solution is to simply add a theme statement after the plot_grid() call:



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




This has created a uniform background of the chosen color. You would of course have to adjust the output dimensions of the plot to avoid the large amount of grey color above and below the two figures.



To highlight more clearly what's happening, let's style the combined plot with a different color choice:





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




We can see that the theme statement is applied to the canvas onto which the two plots are pasted by plot_grid().



Finally, we can ask why the problem exists in the first place, and the answer is because the plots are not aligned. To make them align perfectly, we need to align both vertically and horizontally, and when we do so things work as expected:



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


Normally, align = "h" would be sufficient (align = "v" is incorrect when the plots are placed in the same row), but because the theme has a fixed aspect ratio we need to align both horizontally and vertically, hence align = "vh".









share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 25 '18 at 22:01

























answered Nov 25 '18 at 21:28









Claus WilkeClaus Wilke

7,86742652




7,86742652













  • as noted in my comment to your comment, they aren't aligned

    – hrbrmstr
    Nov 25 '18 at 21:35











  • @hrbrmstr You're correct about the alignment. The plot_grid() call used the wrong align argument. I've addressed this now.

    – Claus Wilke
    Nov 25 '18 at 22:00



















  • as noted in my comment to your comment, they aren't aligned

    – hrbrmstr
    Nov 25 '18 at 21:35











  • @hrbrmstr You're correct about the alignment. The plot_grid() call used the wrong align argument. I've addressed this now.

    – Claus Wilke
    Nov 25 '18 at 22:00

















as noted in my comment to your comment, they aren't aligned

– hrbrmstr
Nov 25 '18 at 21:35





as noted in my comment to your comment, they aren't aligned

– hrbrmstr
Nov 25 '18 at 21:35













@hrbrmstr You're correct about the alignment. The plot_grid() call used the wrong align argument. I've addressed this now.

– Claus Wilke
Nov 25 '18 at 22:00





@hrbrmstr You're correct about the alignment. The plot_grid() call used the wrong align argument. I've addressed this now.

– Claus Wilke
Nov 25 '18 at 22:00


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53370824%2fchange-background-color-with-plot-grid%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini