Skip to content

Panel sizes different across pages even with nrow and ncol defined #473

Description

@LouisMPenrod

I'm trying to make two pages. The first has 8 panels in a 4 row, 2 column layout and the second has 4 panels. In the second, I'd like the panel dimensions to be the same as the first to have visual continuity. However, each panel on the second page is taller then the panels the first page.

library(ggplot2)
library(patchwork)

# 8 identical-style panels
plots <- lapply(1:8, function(i) {
  ggplot(mtcars, aes(wt, mpg)) + geom_point() + ggtitle(paste("Panel", i))
})

# Page 1: all 8, in a 4x2 grid
page1 <- wrap_plots(plots, nrow = 4, ncol = 2)
ggsave("minimal_page1_8panels.png", page1, width = 6, height = 7.68, units = "in", bg = "white")

# Page 2: just the first 4, in the SAME 4x2 grid
page2 <- wrap_plots(plots[1:4], nrow = 4, ncol = 2)
ggsave("minimal_page2_4panels.png", page2, width = 6, height = 7.68, units = "in", bg = "white")
Image

I've tried several other methods I could think of based on the the docs, but all give the same or less desirable plot. I'm very surprised page 6 and 7 give slightly different outputs as I would have expected those would be the same in the background.

# Page 3: Same as 2 but trying to add width and height
page3 <- wrap_plots(plots[1:4], nrow = 4, ncol = 2, widths = 1, heights = 1)
ggsave("minimal_page3_4panels.png", page3, width = 6, height = 7.68, units = "in", bg = "white")

# Page 4: Same as 2 but trying plot_layout's width and height
page4 <- wrap_plots(plots[1:4], nrow = 4, ncol = 2)+
  plot_layout(widths = 1, heights = 1)
ggsave("minimal_page4_4panels.png", page4, width = 6, height = 7.68, units = "in", bg = "white")

# Page 5: Same as 2 but trying design with blanks
layout <- "
AB
CD
##
##
"
page5 <- wrap_plots(plots[1:4], nrow = 4, ncol = 2, design = layout)
ggsave("minimal_page5_4panels.png", page5, width = 6, height = 7.68, units = "in", bg = "white")

# Page 6: Same as 2 but trying with plot_spacer
page6 <- wrap_plots(plots[1:4], plot_spacer(), plot_spacer(), plot_spacer(), plot_spacer(), nrow = 4, ncol = 2)
ggsave("minimal_page6_4panels.png", page6, width = 6, height = 7.68, units = "in", bg = "white")

# Page 7: Same as 6 but not using wrap_plots
page7 <- (plots[[1]] | plots[[2]]) /
  (plots[[3]] | plots[[4]]) /
  (plot_spacer() | plot_spacer()) /
  (plot_spacer() | plot_spacer())
ggsave("minimal_page7_4panels.png", page7, width = 6, height = 7.68, units = "in", bg = "white")

Any help is appreciated.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions