Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Distancing `facet_grid` strips from the faceted canvas?

Tags:

r

ggplot2

Consider the following example from the ?ggplot2::facet_grid:

p <- ggplot(mpg, aes(displ, cty)) + geom_point()
p + facet_grid(vars(drv), vars(cyl))

Which results in the following image enter image description here

What I would like to achieve is a minor offset/white space between the facet-labeling strips and the actual plots. The result would look (sans the gimp involvement): enter image description here

Is that possible using ggplot2 or any of its derivative packages?

Thank you for any insight.

like image 579
balin Avatar asked Jan 01 '23 23:01

balin


2 Answers

Following up on @Tung's anser above (and some documentation-ignoring experimentation), I get it to work:

p <- ggplot(mpg, aes(displ, cty)) + geom_point() 
p +
  facet_grid(vars(drv), vars(cyl)) +
  theme(strip.switch.pad.grid = unit(0.2, "cm"), strip.placement = "outside")

And ... tada!:

enter image description here

like image 152
balin Avatar answered Jan 03 '23 11:01

balin


There is an option strip.switch.pad.grid but it only works if you activate switch argument

library(ggplot2)
theme_set(theme_bw(base_size = 14))

p <- ggplot(mpg, aes(displ, cty)) + geom_point()
p + facet_grid(vars(drv), vars(cyl), 
               switch = 'y') +
  theme(strip.placement = 'outside') +
  theme(strip.switch.pad.grid = unit('0.25', "cm"))

Created on 2019-05-15 by the reprex package (v0.2.1)

like image 21
Tung Avatar answered Jan 03 '23 11:01

Tung