Daniel Valencia C.
Daniel Valencia C.

Reputation: 2279

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

Upvotes: 3

Views: 7713

Answers (3)

Scientist
Scientist

Reputation: 1339

I have a simple cheat-fix if you want to keep the original plot size differences and also align them, based on the answer by @ClausWilke above.

One can always fine-tune plots alignments using cowplot:plot_grid by adding "nested" NULL plots and changing their height/width ratios. See the solution below:

group1<-plot_grid(plot1, plot2, plot3, nrow = 3)
group2<-plot_grid(plot4, plot5, plot6, plot7, NULL, rel_heights = c(1,1, 1,1,0.001), nrow = 5)
plot_grid(group1, group2, align='h', nrow=1)

This is the output: enter image description here

This has helped me a lot in those cases where axis parameter will not do the job.

Upvotes: 0

Claus Wilke
Claus Wilke

Reputation: 17790

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

Upvotes: 5

Aaron - mostly inactive
Aaron - mostly inactive

Reputation: 37754

I had do exactly this just last week! Try doing just one call to plot_grid, with NULL where you want the empty spot.

myplotlist <- list(plot1, plot2, plot3, NULL, plot4, plot5, plot6, plot7, plot8)
plot_grid(plotlist=myplotlist, align = "v", ncol = 2)

Upvotes: 4

Related Questions