Rikard N
Rikard N

Reputation: 447

How to keep the size of individual plots in a facet plot in R

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

Upvotes: 2

Views: 1456

Answers (2)

Rikard N
Rikard N

Reputation: 447

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.

Upvotes: 0

user10756946
user10756946

Reputation: 31

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)

Upvotes: 3

Related Questions