typestar

Aesthetics in R

Color, shape and size can carry data, or be fixed constants.

library(ggplot2)
library(tibble)

runs <- tibble(
  day = rep(1:3, 2),
  tpm = c(88, 92, 96, 99, 104, 101),
  lang = rep(c("r", "python"), each = 3),
  errors = c(3, 2, 1, 0, 1, 2)
)

mapped <- ggplot(runs, aes(day, tpm, colour = lang, size = errors)) +
  geom_point()

fixed <- ggplot(runs, aes(day, tpm, group = lang)) +
  geom_line(colour = "steelblue", linewidth = 1)

print(names(mapped$mapping))
print(length(fixed$layers))
ggsave(tempfile(fileext = ".png"), mapped, width = 4, height = 3, dpi = 100)

How it works

  1. Inside aes a value maps a column; outside it sets a constant.
  2. That distinction is the most common ggplot mistake.
  3. group controls which rows join up.

Keywords and builtins used here

The run, in numbers

Lines
19
Characters to type
494
Tokens
171
Three-star pace
95 tpm

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

Type this snippet

Step 2 of 3 in The grammar, step 2 of 15 in ggplot2.

← Previous Next →