Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I scale nested plot_grid to the same size?

I have 7 plots and I organize them in 2 columns, one with 3 plots and other one with 4 plots. I used plot_grid from cowplot for that. The result is almost perfect, however, the column with 3 plots has bigger plots. How can I scale this column to get the same size in all plots and align the first and the last plot of each column?

library(ggplot2)
library(cowplot)

Value <- seq(0,1000, by = 1000/10)
Index <- 0:10
DF <- data.frame(Index, Value)

plot1 <- ggplot(DF, aes(x = Index, y = Value)) +
  geom_line(linetype = 2) +
  theme(aspect.ratio = 0.5)

plot2 <- ggplot(DF, aes(x = Index, y = Value)) +
  geom_line(linetype = 2) +
  theme(aspect.ratio = 0.5)

plot3 <- ggplot(DF, aes(x = Index, y = Value)) +
  geom_line(linetype = 2) +
  theme(aspect.ratio = 0.5)

plot4 <- ggplot(DF, aes(x = Index, y = Value)) +
  geom_line(linetype = 2) +
  theme(aspect.ratio = 0.5)

plot5 <- ggplot(DF, aes(x = Index, y = Value)) +
  geom_line(linetype = 2) +
  theme(aspect.ratio = 0.5)

plot6 <- ggplot(DF, aes(x = Index, y = Value)) +
  geom_line(linetype = 2) +
  theme(aspect.ratio = 0.5)

plot7 <- ggplot(DF, aes(x = Index, y = Value)) +
  geom_line(linetype = 2) +
  theme(aspect.ratio = 0.5)

col1 <- plot_grid(plot1, plot2, plot3, align = "v", ncol = 1)
col2 <- plot_grid(plot4, plot5, plot6, plot7, align = "v", ncol = 1)
plot_grid(col1, col2, ncol = 2, align = "hv")

enter image description here

like image 260
Daniel Valencia C. Avatar asked Jan 17 '26 05:01

Daniel Valencia C.


1 Answers

You can place a null plot into your first column:

col1 <- plot_grid(plot1, plot2, plot3, NULL, align = "v", ncol = 1)

However, to me, this is not a case for a nested plot grid. Nested plot grids are specifically for the purpose of combining plots with complex arrangements, e.g. one plot in one column spanning two rows in another column. For what you want, I would do a single plot_grid() call, just like the other comment suggests:

plot_grid(plot1, plot4, plot2, plot5, plot3, plot6, NULL, plot7,
          ncol = 2, align = "hv")

enter image description here

Please also note that the align option doesn't work when you specify a particular aspect ratio. You can either specify an aspect ratio or align. In most cases, it's not necessary to specify an aspect ratio, you can do that when saving the figure. The function save_plot() in particular takes an aspect ratio option (applied to the whole image, though, not the plot area).

like image 159
Claus Wilke Avatar answered Jan 19 '26 20:01

Claus Wilke



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!