ggplot: How to increase spacing between faceted plots?

87,994

Solution 1

Use the theme function:

library(grid)

p + theme(panel.spacing = unit(2, "lines"))

See also here: Slicing plots generated by ggplot2

Solution 2

Just to add to @rcs response:

# Change spacing between facets on both axis
p + theme(panel.spacing = unit(2, "lines"))

# Change horizontal spacing between facets
p + theme(panel.spacing.x = unit(2, "lines"))

# Change vertical spacing between facets
p + theme(panel.spacing.y = unit(2, "lines"))
Share:
87,994
wishihadabettername
Author by

wishihadabettername

Updated on July 26, 2022

Comments

  • wishihadabettername
    wishihadabettername over 1 year

    I have several faceted histograms (obtained with the command below) which are nicely plotted one under the other. I would like to increase the spacing between them, however, they are tight.

    I looked at the doc but didn't find a parameter for this.

    qplot (Happiness.Level, Number.of.Answers, data=mydata, geom="histogram") + facet_grid (Location ~ .) 
    
    • wishihadabettername
      wishihadabettername about 13 years
      A side comment that I found another way to approach this (used a faced_wrap instead of facet_grid and the resulting chart is better looking). I'm still interested if there is a way to address the original question.
    • Brandon Bertelsen
      Brandon Bertelsen about 13 years
      Try theme_get() for a slew of hidden options that you can adjust with opts()
  • user4786271
    user4786271 over 8 years
  • David J. Harris
    David J. Harris about 7 years
    Due to another update, the incantation is now theme(panel.margin = unit(2, "lines"))
  • Mr.ecos
    Mr.ecos over 6 years
    Update - is now theme(panel.spacing = unit(1, "lines"))
  • PatrickT
    PatrickT about 6 years
    This works without the grid package. In my experience the x labels of two side-by-side plots often overlap (the greatest label on the lhs and the smallest label on the rhs). One solution to fix the output is to ggsave with a greater width than the default. But I find theme(panel.spacing = unit(1, "lines")) nicer because it also works for the pop-up plot window.
  • Omar Wasow
    Omar Wasow almost 2 years
    I was getting an error "Error: widths must be a unit object" but changing from "lines" to "pt" worked as in theme(panel.spacing = unit(15, "pt").