9.2 Changing the Appearance of Text

9.2.1 Problem

You want to change the appearance of text in a plot.

9.2.3 Discussion

There are two kinds of text items in ggplot2: theme elements and text geoms. Theme elements are all the non-data elements in the plot: the title, legends, and axes. Text geoms are things that are part of the plot itself, and reflect the data content.

There are differences in the parameters, as shown in Table 9.1.

Table 9.1: Text properties of theme elements and text geoms
Theme elements Text geoms Description
family family Helvetica, Times, Courier
face fontface plain, bold, italic, bold.italic
colour colour Color (name or “#RRGGBB”)
size size Font size (in points for theme elements; in mm for geoms)
hjust hjust Horizontal alignment: 0 = left, 0.5 = center, 1 = right
vjust vjust Vertical alignment: 0 = bottom, 0.5 = middle, 1 = top
angle angle Angle in degrees
lineheight lineheight Line spacing multiplier

The theme elements are listed in Table 9.2. Most of them are straightforward. Some are shown in Figure 9.6.

Table 9.2: Theme items that control text appearance in theme()
Element name Description
axis.title Appearance of axis labels on both axes
axis.title.x Appearance of x-axis label
axis.title.y Appearance of y-axis label
axis.ticks Appearance of tick labels on both axes
axis.ticks.x Appearance of x tick labels
axis.ticks.y Appearance of y tick labels
legend.title Appearance of legend title
legend.text Appearance of legend items
plot.title Appearance of overall plot title
strip.text Appearance of facet labels in both directions
strip.text.x Appearance of horizontal facet labels
strip.text.y Appearance of vertical facet labels
Aligning with hjust and vjust, and spacing with lineheight

Figure 9.6: Aligning with hjust and vjust, and spacing with lineheight