Search code examples
rggplot2facet-wrap

How can I append facets and keep individual facet height in the plot as is?


I want to create a large facet_wrap with ggplot2.

What I want is some automatic way to append the individual facets by column so that the facets keep the size they would have by default if (with the same data) only the first row with four columns of facets would be supplied.

E.g. I am concerned about the plot height. I know how to manually change the plot height, but I need an automatic way.

Some example data:

mpg <- mpg %>%
distinct(model, year, .keep_all = TRUE)

ggplot(mpg, aes(x=year, y=hwy))+
   geom_point()+
   facet_wrap(~model)

All facets should have the same height as

mpg %>%
  distinct(model, year, .keep_all = TRUE) %>%
  filter(model %in% c("4runner 4wd", "a4", "a4 quattro", "altima")) %>%

  ggplot(aes(x=year, y=hwy))+
  geom_point()+
  facet_wrap(~model, ncol = 4)

Solution

  • You can conserve plot height by using theme(aspect.ratio). For example,

    mpg <- mpg %>%
        distinct(model, year, .keep_all = TRUE)
    
    ggplot(mpg, aes(x=year, y=hwy))+
        geom_point()+
        facet_wrap(~model) + theme(aspect.ratio=2)
    

    aspect_ratio