typestar

Scales in R

Scales control the mapping from data to pixels, colors and breaks.

library(ggplot2)
library(tibble)

runs <- tibble(
  seconds = c(30, 60, 300, 600, 1500),
  chars = c(400, 900, 4200, 8000, 19000)
)

plot <- ggplot(runs, aes(seconds, chars)) +
  geom_point(size = 3) +
  scale_x_log10(breaks = c(30, 60, 300, 1500),
                labels = c("30s", "1m", "5m", "25m")) +
  scale_y_continuous(limits = c(0, 20000),
                     breaks = seq(0, 20000, 5000),
                     labels = function(x) paste0(x / 1000, "k")) +
  labs(x = "sprint length", y = "characters typed")

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

How it works

  1. Every aesthetic has a family of scale functions.
  2. limits clips; expand pads.
  3. A log scale is a scale, not a transformation of the data.

Keywords and builtins used here

The run, in numbers

Lines
19
Characters to type
557
Tokens
182
Three-star pace
105 tpm

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

Type this snippet

Step 2 of 4 in Panels & scales, step 9 of 15 in ggplot2.

← Previous Next →