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"))
Author by
wishihadabettername
Updated on July 26, 2022Comments
-
wishihadabettername 11 months
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 over 12 yearsA 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 over 12 yearsTry theme_get() for a slew of hidden options that you can adjust with opts()
-
-
user4786271 almost 8 years
-
David J. Harris over 6 yearsDue to another update, the incantation is now
theme(panel.margin = unit(2, "lines"))
-
Mr.ecos over 6 yearsUpdate - is now
theme(panel.spacing = unit(1, "lines"))
-
PatrickT over 5 yearsThis 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 toggsave
with a greater width than the default. But I findtheme(panel.spacing = unit(1, "lines"))
nicer because it also works for the pop-up plot window. -
Omar Wasow over 1 yearI 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")
.