9.4 Changing the Appearance of Theme Elements

9.4.1 Problem

You want to change the appearance of theme elements.

9.4.2 Solution

To modify a theme, add theme() with a corresponding element_xx object. These include element_line, element_rect, and element_text. The following code shows how to modify many of the commonly used theme properties (Figure 9.9):

Clockwise from top left: modifying theme properties for the plotting area, the legend, the facets, and the text itemsClockwise from top left: modifying theme properties for the plotting area, the legend, the facets, and the text itemsClockwise from top left: modifying theme properties for the plotting area, the legend, the facets, and the text itemsClockwise from top left: modifying theme properties for the plotting area, the legend, the facets, and the text items

Figure 9.9: Clockwise from top left: modifying theme properties for the plotting area, the legend, the facets, and the text items

9.4.3 Discussion

If you want to use a saved theme and tweak a few parts of it with theme(), the theme() must come after the theme specification. Otherwise, anything set by theme() will be unset by the theme you add:

Many of the commonly used theme properties are shown in Table 9.3.

Table 9.3: Theme items that control text appearance in theme()
Name Description Element type
text All text elements element_text()
rect All rectangular elements element_rect()
line All line elements element_line()
axis.line Lines along axes element_line()
axis.title Appearance of both axis labels element_text()
axis.title.x X-axis label appearance element_text()
axis.title.y Y-axis label appearance element_text()
axis.text Appearance of tick labels on both axes element_text()
axis.text.x X-axis tick label appearance element_text()
axis.text.y Y-axis tick label appearance element_text()
legend.background Background of legend element_rect()
legend.text Legend item appearance element_text()
legend.title Legend title appearance element_text()
legend.position Position of the legend "left", "right", "bottom", "top", or two-element numeric vector if you wish to place it inside the plot area (for more on legend placement, see Recipe 10.2)
panel.background Background of plotting area element_rect()
panel.border Border around plotting area element_rect(linetype="dashed")
panel.grid.major Major grid lines element_line()
panel.grid.major.x Major grid lines, vertical element_line()
panel.grid.major.y Major grid lines, horizontal element_line()
panel.grid.minor Minor grid lines element_line()
panel.grid.minor.x Minor grid lines, vertical element_line()
panel.grid.minor.y Minor grid lines, horizontal element_line()
plot.background Background of the entire plot element_rect(fill = "white", colour = NA)
plot.title Title text appearance element_text()
strip.background Background of facet labels element_rect()
strip.text Text appearance for vertical and horizontal facet labels element_text()
strip.text.x Text appearance for horizontal facet labels element_text()
strip.text.y Text appearance for vertical facet labels element_text()