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
scale_color_manualtakes a named vector of colors.scale_fill_viridis_cis the perceptually even continuous option.- The British and American spellings both work.
Keywords and builtins used here
aescclassgeom_colgeom_pointggplotggsavelibraryprintrepscale_colour_manualscale_fill_viridis_ctempfiletibble
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.
Step 3 of 4 in Panels & scales, step 10 of 15 in ggplot2.