2

I try to let a big facet plot span over multiple pages, but have problems with the final page. Depending on the number of subplots that end up on the final page they change in size so that they don't have the same size as the previous pages. How can I get all of the subplots to have the same size? See my example using ggforce to see my problem. Answers does not have to use ggforce it was just the easiest way to show my issue.

library(ggplot2)
library(ggforce)
df <- data.frame(x=rnorm(100, 1, 1), y=rnorm(100,1,1), group=rep(c(1,2), 100), 
item=rep(c(1,2,3,4,5), 40))
ggplot(df) +
    geom_point(aes(x, y)) +
    facet_grid_paginate(item~group, ncol = 2, nrow = 3, page = 1)
ggplot(df) +
    geom_point(aes(x, y)) +
    facet_grid_paginate(item~group, ncol = 2, nrow = 3, page = 2)

This code will generate the two pages below. Note that the subplots of page 2 are bigger than those in page 1. I would like the same size and a blank area at the bottom. I would also like to keep one set of axes and labels per page.

The first page The second page

Here

Rikard N
  • 427
  • 4
  • 16

2 Answers2

3

egg::set_panel_size() can be used to set the panel size to a fixed value, e.g.

library(ggplot2)
library(egg)
library(gridExtra)
df <- data.frame(x=rnorm(100, 1, 1), y=rnorm(100,1,1), group=rep(c(1,2), 100), 
                 item=rep(c(1,2,3,4,5), 40))

sd <- split(df, cut(df$item, 2))

p <- ggplot(df) +  geom_point(aes(x, y)) + facet_grid(item~group) + theme_grey()
pl <- lapply(sd, "%+%", e1 = p)
pl <- lapply(pl, egg::set_panel_size, width=unit(2,"in"), height = unit(1.5,"in"))

marrangeGrob(pl, nrow=1,ncol=1)
  • Thanks, this idea is interesting! This is quite close to what I want. I would like the plots on the final page to have the same alignment as the others, i.e. to be a bit further up on the page, The margins are too wide and I don't like the "page 1 of 2" at the top of each page. I will experiment a bit with this. Sorry for being picky! – Rikard N Dec 07 '18 at 07:55
0

This issue has now been solved in a later ggforce version (I tested it to work on version 0.2.2). The code in the question will now work as wanted.

Rikard N
  • 427
  • 4
  • 16