10.6 Changing the Appearance of a Legend Title

10.6.1 Problem

You want to change the appearance of a legend title’s text.

10.6.2 Solution

Use theme(legend.title = element_text()) (Figure 10.11):

pg_plot <- ggplot(PlantGrowth, aes(x = group, y = weight, fill = group)) +
  geom_boxplot()

pg_plot +
  theme(legend.title = element_text(
    face = "italic",
    family = "Times",
    colour = "red",
    size = 14)
  )
Customized legend title appearance

Figure 10.11: Customized legend title appearance

10.6.3 Discussion

It’s also possible to specify the legend title’s appearance via guides(), but this method can be a bit verbose. This has the same effect as the previous code:

pg_plot +
  guides(fill = guide_legend(title.theme = element_text(
    face = "italic",
    family = "times",
    colour = "red",
    size = 14))
  )

10.6.4 See Also

See Recipe 9.2 for more on controlling the appearance of text.