10.9 Changing the Appearance of Legend Labels

10.9.1 Problem

You want to change the appearance of labels in a legend.

10.9.2 Solution

Use theme(legend.text=element_text()) (Figure 10.16):

# Create the base plot
pg_plot <- ggplot(PlantGrowth, aes(x = group, y = weight, fill = group)) +
  geom_boxplot()

# Change the legend label appearance
pg_plot +
  theme(legend.text = element_text(
    face = "italic",
    family = "Times",
    colour = "red",
    size = 14)
  )
Customized legend label appearance

Figure 10.16: Customized legend label appearance

10.9.3 Discussion

It’s also possible to specify the legend label appearance via guides(), although this method is a bit unwieldy. This has the same effect as the previous code:

# Changes the legend title text for the fill legend
pg_plot +
  guides(fill = guide_legend(title.theme = element_text(
    face = "italic",
    family = "times",
    colour = "red",
    size = 14))
  )

10.9.4 See Also

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