11.3 Changing the Text of Facet Labels

11.3.1 Problem

You want to change the text of facet labels.

11.3.3 Discussion

Unlike with scales where you can set the labels, to set facet labels you must change the data values. Also, at the time of this writing, there is no way to show the name of the faceting variable as a header for the facets, so it can be useful to use descriptive facet labels.

With facet_grid() but not facet_wrap(), at this time), it’s possible to use a labeller function to set the labels. The labeller function label_both() will print out both the name of the variable and the value of the variable in each facet (Figure 11.5, left):

Another useful labeller is label_parsed(), which takes strings and treats them as R math expressions (Figure 11.5, right):

With label_both() (left); With label_parsed() for mathematical expressions (right)With label_both() (left); With label_parsed() for mathematical expressions (right)

Figure 11.5: With label_both() (left); With label_parsed() for mathematical expressions (right)

11.3.4 See Also

See Recipe 15.10 for more on renaming factor levels. If the faceting variable is not a factor but a character vector, changing the values is somewhat different. See Recipe 15.12 for information on renaming items in character vectors.