typestar

Themes in R

A theme is every non-data element: fonts, grids, legends, margins.

library(ggplot2)
library(tibble)

runs <- tibble(day = 1:5, tpm = c(88, 92, 96, 99, 104))

plot <- ggplot(runs, aes(day, tpm)) +
  geom_col(fill = "gray40") +
  theme_minimal(base_size = 11) +
  theme(
    panel.grid.minor = element_blank(),
    panel.grid.major.x = element_blank(),
    axis.title = element_text(colour = "gray30"),
    plot.title = element_text(face = "bold", size = 13),
    legend.position = "none"
  ) +
  labs(title = "Minimal, then adjusted")

print(inherits(plot$theme, "theme"))
ggsave(tempfile(fileext = ".png"), plot, width = 4, height = 3, dpi = 100)

How it works

  1. A complete theme like theme_minimal replaces the defaults.
  2. theme() then adjusts individual elements.
  3. theme_set applies one for the whole session.

Keywords and builtins used here

The run, in numbers

Lines
19
Characters to type
549
Tokens
149
Three-star pace
105 tpm

At the three-star pace of 105 tokens a minute, this run takes about 85 seconds.

Type this snippet

Step 2 of 3 in Finishing a plot, step 13 of 15 in ggplot2.

← Previous Next →