Search code examples
rggplot2cowplot

Setting width and height of a single panel in multi-panel plot in cowplot::plot_grid


I am making a multi-panel plot using ggplot2 and cowplot packages, but I need to change the height of a single plot. It is easiest shown with an example

library(ggplot2)
library(cowplot)

p1 <- ggplot(iris, aes(Sepal.Width, Sepal.Length, colour = Species)) + 
             geom_point() +
             theme(axis.text.x = element_blank(),
                   axis.title.x = element_blank(),
                   legend.position = "none")
p2 <- ggplot(iris, aes(Sepal.Width, Sepal.Length, colour = Species)) + 
             geom_point() +
             theme(axis.text.x = element_blank(),
                   axis.title.x = element_blank(),
                   axis.text.y = element_blank(),
                   axis.title.y = element_blank(),
                   legend.position = "none")
p3 <- ggplot(iris, aes(Sepal.Width, Sepal.Length, colour = Species)) + 
             geom_point() +
             theme(axis.text.y = element_blank(),
                   axis.title.y = element_blank(),
                   legend.position = "none")
p4 <- ggplot(iris, aes(Sepal.Width, Sepal.Length, colour = Species)) + 
             geom_point() + 
             theme(legend.position = "none")
p5 <- ggplot(iris, aes(Sepal.Width, Sepal.Length, colour = Species)) + 
             geom_point() +
             theme(axis.text.y = element_blank(),
                   axis.title.y = element_blank(),
                   legend.position = "none")

pL <- ggplot(iris, aes(Sepal.Width, Sepal.Length, colour = Species)) + geom_point()
l <- get_legend(pL)

# simple grid
plot_grid(p1, p2, p3, p4, p5, l, ncol = 3)

enter image description here

As you can see the y axis in the far right top panel has been shrunk in comparison with the two other panels in the same row by the inclusion of the x-axis title.

So how do I set the relative height and width of this single panel, so that the y-axis aligns with the y-axis of the panels in the top row?

You cannot set individual panels using rel_heights = and rel_widths() = arguments, and when I tried to add axis = "tblr" and align = "hv" arguments I got the error message

Error in `[.unit.list`(sizes[[x]], list_indices[[x]]) : 
  index out of bounds (unit list subsetting) 

Solution

  • patchwork package can get the job done. egg or multipanelfigure packages might work too.

    # https://github.com/thomasp85/patchwork
    # install.packages("devtools", dependencies = TRUE)
    # devtools::install_github("thomasp85/patchwork")
    library(patchwork)
    #> 
    #> Attaching package: 'patchwork'
    #> The following object is masked from 'package:cowplot':
    #> 
    #>     align_plots
    
    layout <- "
    ABC
    DEF
    "
    p1 + p2 + p3 + p4 + p5 + l +
      plot_layout(design = layout)
    

    (p1 + p2 + p3)/(p4 + p5 + l) +
      plot_layout(nrow = 2) +
      plot_annotation(title = "Title",
                      subtitle = "Subtitle",
                      tag_levels = 'i',
                      tag_suffix = ')')
    

    Created on 2020-03-26 by the reprex package (v0.3.0)