115

I'm wondering how I can manipulate the size of strip text in facetted plots. My question is similar to a question on plot titles, but I'm specifically concerned with manipulating not the plot title but the text that appears in facet titles (strip_h).

As an example, consider the mpg dataset.

    library(ggplot2) 
    qplot(hwy, cty, data = mpg) + facet_grid( . ~ manufacturer)

The resulting output produces some facet titles that don't fit in the strip.

I'm thinking there must be a way to use grid to deal with the strip text. But I'm still a novice and wasn't sure from the grid appendix in Hadley's book how, precisely, to do it.

starball
  • 20,030
  • 7
  • 43
  • 238
briandk
  • 6,749
  • 8
  • 36
  • 46

3 Answers3

151

You can modify strip.text.x (or strip.text.y) using theme_text(), for instance

qplot(hwy, cty, data = mpg) + 
      facet_grid(. ~ manufacturer) + 
      opts(strip.text.x = theme_text(size = 8, colour = "red", angle = 90))

Update: for ggplot2 version > 0.9.1

qplot(hwy, cty, data = mpg) + 
      facet_grid(. ~ manufacturer) + 
      theme(strip.text.x = element_text(size = 8, colour = "red", angle = 90))
rcs
  • 67,191
  • 22
  • 172
  • 153
  • +1 Very nice. Is there a way to improve also the x axis annotation? – gd047 May 01 '10 at 19:18
  • Thanks rcs. I'm with gd047, and perhaps that should be a separate question? What you're noticing is the crowding of major x-values, I think. There should be a way to set the values of the major x-axis and y-axis tick marks manually, but I can't remember what it is :-( – briandk May 01 '10 at 19:50
  • @gd047: `axis.text.x=theme_text(...)` – rcs May 01 '10 at 19:56
  • 1
    Links in this Q should be somewhat informative: http://stackoverflow.com/questions/2258784/list-of-ggplot2-options – Roman Luštrik Mar 20 '12 at 08:36
  • Does this change the text, or the box the text is written in. Because I just get a big box. – Harlan Nelson Dec 19 '22 at 01:59
37

Nowadays the usage of opts and theme_text seems to be deprecated. R suggests to use theme and element_text. A solution to the answer can be found here: http://wiki.stdout.org/rcookbook/Graphs/Facets%20%28ggplot2%29/#modifying-facet-label-text

qplot(hwy, cty, data = mpg) + 
      facet_grid(. ~ manufacturer) + 
      theme(strip.text.x = element_text(size = 8, colour = "red", angle = 90))
moi
  • 1,835
  • 2
  • 18
  • 25
6

I guess in the example of mpg changing the rotation angle and font size is fine, but in many cases you might find yourself with variables that have quite lengthy labels, and it can become a pain in the neck (literally) to try read rotated lengthy labels.

So in addition (or complement) to changing angles and sizes, I usually reformat the labels of the factors that define the facet_grid whenever they can be split in a way that makes sense.

Typically if I have a dataset$variable with strings that looks like

c("median_something", "aggregated_average_x","error","something_else")

I simply do:

reformat <– function(x,lab="\n"){ sapply(x, function(c){ paste(unlist(strsplit(as.character(c) , split="_")),collapse=lab) }) }

[perhaps there are better definitions of reformat but at least this one works fine.]

dataset$variable <- factor(dataset$variable, labels=reformat(dataset$variable, lab='\n')

And upon facetting, all labels will be very readable:

ggplot(data=dataset, aes(x,y)) + geom_point() + facet_grid(. ~ variable)
G Chalancon
  • 306
  • 3
  • 7
  • 1
    Maybe something like this is better: levels(birds$effect) <- gsub(" ", "\n", levels(birds$effect)) – dca Jul 08 '17 at 22:27