Theme

Andrew Irwin, a.irwin@dal.ca

2024-03-28

Plan

  • What is the theme of a visualization?

  • Ready-to-use themes

  • Text size

  • Text colour, bold, italics, and more

  • Legend spacing and positioning

  • Formatting axis numbers

Starting figure

Ready to use themes

p1 + theme_bw()

Ready to use themes

p1 + theme_classic()

More themes

  • theme_dark, theme_gray, theme_light

  • From ggthemes: theme_wsj, theme_tufte, theme_fivethirtyeight, theme_economist, theme_clean

All text larger

p1 + theme(text = element_text(size=22))

Sizes for different elements

p1 + theme(axis.text.x = element_text(size=16),
           axis.title.y = element_text(size=18),
           legend.title = element_text(size=16),
           legend.text = element_text(size=18),
           plot.title = element_text(size=21),
           plot.subtitle = element_text(size=20)) +
  labs(x = "Year", 
       y = "Life Expectancy at birth",
       title = "Life expectancy averaged by continent",
       subtitle = "From Gapminder.org")

Other features of text

p1 + labs(title = "Average life expectancy") + 
  theme(plot.title = element_text(family = "serif",
                                  face = "italic",
                                  color = "blue",
                                  size = 30,
                                  hjust = 1, 
                                  lineheight = 2))

Fancy text: ggtext

library(ggtext)
p1 + labs(title = "**Gapminder** data for _life expectancy_<sup>1</sup>",
          caption = "<sup>1</sup>from gapminder.org") +
  theme(plot.title = element_markdown(size = 24),
        plot.caption = element_markdown(size = 13))

Move and redesign the legend

p1 + theme(legend.position = "top",
           legend.direction = "horizontal",
           legend.text = element_text(size = 11, vjust = 0.5),
           legend.title = element_text(size = 12, vjust = 0.5),
           legend.key.size = unit(0.9, "cm"))

Move and redesign the legend

p1 + theme_bw() + theme(legend.position = c(0.03, 0.98),
                        legend.justification = c(0,1),
           legend.direction = "vertical",
           legend.background = element_rect(fill = "transparent"),
           legend.text = element_text(size = 10, vjust = 0.5),
           legend.title = element_blank(),
           legend.key.size = unit(0.8, "cm")) + 
  guides(color = guide_legend(ncol = 3))

Formatting numbers on scales

p1 + scale_x_continuous(labels = scales::dollar) +
  scale_y_continuous(labels = scales::percent) + # also comma
  theme(axis.text = element_text(size=14))

Formatting numbers on scales

p2 + scale_y_continuous(trans = "log2", labels = scales::scientific,
                        breaks = c(20,40,60,80)) +
  scale_x_continuous(trans = "date", label = label_date(format="%Y-%m")) + 
  theme(axis.text = element_text(size=14))

Power of 10 notation

tibble(x=10^seq(0,15), y = x*1e-15) |> ggplot(aes(x , y)) + geom_blank() + 
  scale_x_log10(labels = trans_format('log10', math_format(10^.x)),
                breaks = c(1e0,1e3,1e6,1e9,1e12,1e15)) + 
  scale_y_log10(labels = trans_format('log10', math_format(10^.x)),
                breaks = c(1e-15,1e-12,1e-9,1e-6,1e-3,1e0))

SI units

tibble(x=10^seq(0,15), y = x*1e-15) |> ggplot(aes(x , y)) + geom_blank() + 
  scale_x_continuous(trans = "log10", labels = label_number(scale_cut = cut_si("g")),
                     breaks = c(1e0,1e3,1e6,1e9,1e12,1e15)) +
  scale_y_continuous(trans = "log10", labels = label_number(scale_cut = cut_si("m")),
                     breaks = c(1e-15,1e-12,1e-9,1e-6,1e-3,1e0))

Getting help with theme

?theme

theme(line, rect, text, title, aspect.ratio, axis.title, axis.title.x, axis.title.x.top, axis.title.x.bottom, axis.title.y, axis.title.y.left, axis.title.y.right, axis.text, axis.text.x, axis.text.x.top, axis.text.x.bottom, axis.text.y, axis.text.y.left, axis.text.y.right, axis.ticks, axis.ticks.x, axis.ticks.x.top, axis.ticks.x.bottom, axis.ticks.y, axis.ticks.y.left, axis.ticks.y.right, axis.ticks.length, axis.ticks.length.x, axis.ticks.length.x.top, axis.ticks.length.x.bottom, axis.ticks.length.y, axis.ticks.length.y.left, axis.ticks.length.y.right, axis.line, axis.line.x, axis.line.x.top, axis.line.x.bottom, axis.line.y, axis.line.y.left, axis.line.y.right, legend.background, legend.margin, legend.spacing, legend.spacing.x, legend.spacing.y, legend.key, legend.key.size, legend.key.height, legend.key.width, legend.text, legend.text.align, legend.title, legend.title.align, legend.position, legend.direction, legend.justification, legend.box, legend.box.just, legend.box.margin, legend.box.background, legend.box.spacing, panel.background, panel.border, panel.spacing, panel.spacing.x, panel.spacing.y, panel.grid, panel.grid.major, panel.grid.minor, panel.grid.major.x, panel.grid.major.y, panel.grid.minor.x, panel.grid.minor.y, panel.ontop, plot.background, plot.title, plot.title.position, plot.subtitle, plot.caption, plot.caption.position, plot.tag, plot.tag.position, plot.margin, strip.background, strip.background.x, strip.background.y, strip.clip, strip.placement, strip.text, strip.text.x, strip.text.x.bottom, strip.text.x.top, strip.text.y, strip.text.y.left, strip.text.y.right, strip.switch.pad.grid, strip.switch.pad.wrap)

Theme elements

element_blank()

element_rect( fill = NULL, colour = NULL, linewidth = NULL, linetype = NULL, color = NULL)

element_line( colour = NULL, linewidth = NULL, linetype = NULL, lineend = NULL, color = NULL, arrow = NULL)

element_text( family = NULL, face = NULL, colour = NULL, size = NULL, hjust = NULL, vjust = NULL, angle = NULL, lineheight = NULL, color = NULL, margin = NULL)

Further reading

Chapter on Theme in the ggplot book (currently being revised.)