typestar

Facets in R

Small multiples: the same plot, once per group.

library(ggplot2)
library(tibble)

runs <- tibble(
  day = rep(1:5, 4),
  tpm = c(88:92, 99:103, 70:74, 60:64),
  lang = rep(c("r", "python", "sql", "css"), each = 5),
  editor = rep(c("on", "off"), each = 10)
)

wrapped <- ggplot(runs, aes(day, tpm)) +
  geom_line() +
  facet_wrap(~ lang, ncol = 2, scales = "free_y")

gridded <- ggplot(runs, aes(day, tpm)) +
  geom_point() +
  facet_grid(editor ~ lang)

print(class(wrapped$facet)[1])
ggsave(tempfile(fileext = ".png"), wrapped, width = 5, height = 4, dpi = 100)

How it works

  1. facet_wrap wraps a single variable into a grid.
  2. facet_grid puts one variable on rows and another on columns.
  3. scales = \u0022free_y\u0022 lets each panel size its own axis.

Keywords and builtins used here

The run, in numbers

Lines
20
Characters to type
499
Tokens
178
Three-star pace
105 tpm

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

Type this snippet

Step 1 of 4 in Panels & scales, step 8 of 15 in ggplot2.

← Previous Next →