typestar

Color scales in R

Discrete palettes, continuous gradients, and naming colors yourself.

library(ggplot2)
library(tibble)

runs <- tibble(
  day = 1:6,
  tpm = c(88, 92, 96, 99, 104, 101),
  lang = rep(c("r", "python"), 3),
  accuracy = c(96, 97, 97.5, 98, 99, 98.5)
)

manual <- ggplot(runs, aes(day, tpm, colour = lang)) +
  geom_point(size = 3) +
  scale_colour_manual(values = c(r = "#7c5cff", python = "#e06c75"))

continuous <- ggplot(runs, aes(day, tpm, fill = accuracy)) +
  geom_col() +
  scale_fill_viridis_c(option = "magma")

print(class(manual$scales$scales[[1]])[1])
ggsave(tempfile(fileext = ".png"), continuous, width = 4, height = 3,
       dpi = 100)

How it works

  1. scale_color_manual takes a named vector of colors.
  2. scale_fill_viridis_c is the perceptually even continuous option.
  3. The British and American spellings both work.

Keywords and builtins used here

The run, in numbers

Lines
21
Characters to type
556
Tokens
180
Three-star pace
105 tpm

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

Type this snippet

Step 3 of 4 in Panels & scales, step 10 of 15 in ggplot2.

← Previous Next →