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
facet_wrapwraps a single variable into a grid.facet_gridputs one variable on rows and another on columns.scales = \u0022free_y\u0022lets each panel size its own axis.
Keywords and builtins used here
aescclassfacet_gridfacet_wrapgeom_linegeom_pointggplotggsavelibraryprintreptempfiletibble
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.
Step 1 of 4 in Panels & scales, step 8 of 15 in ggplot2.