15

I would like the grey bar at the top to be wider, as in, have the edges of it a little further from the top and bottom of the letters (the strip.text - A, B, C etc). I would have thought the lineheight would have acted as padding but it doesn't.

ggplot(diamonds, aes(carat, price, fill = ..density..)) +
  xlim(0, 2) + stat_binhex(na.rm = TRUE)+
  facet_wrap(~ color) +
  theme(strip.text = element_text(lineheight=20)) 
nzcoops
  • 9,132
  • 8
  • 41
  • 52

2 Answers2

24

First, modify the levels so that they include a linebreak:

levels(diamonds$color) <- paste0(" \n", levels(diamonds$color) , "\n ")

Then adjust as necessary. eg:

P <- ggplot(diamonds, aes(carat, price, fill = ..density..)) +
      xlim(0, 2) + stat_binhex(na.rm = TRUE)+
      facet_wrap(~ color)

P +  theme(strip.text = element_text(size=9, lineheight=0.5))

lineheight=0.5

P +  theme(strip.text = element_text(size=9, lineheight=3.0))

lineheight=3.0

Ricardo Saporta
  • 54,400
  • 17
  • 144
  • 178
  • 1
    Delayed reply on my part... Thanks, a dirty solution let's be honest haha, but certainly get's the job done. Appreciate your help. – nzcoops Aug 29 '13 at 00:27
1

With ggplot2 2.2.0, you can apply Ricardo's trick without modifying the dataframe, by using labeller:

P <- ggplot(diamonds, aes(carat, price, fill = ..density..)) +
  xlim(0, 2) + stat_binhex(na.rm = TRUE)+
  facet_wrap(~ color, labeller = labeller(color=setNames(paste0("\n", levels(diamonds$color), "\n"), levels(diamonds$color))))

P +  theme(strip.text = element_text(size=9, lineheight=0.5))
Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225