Part 58 Color scales
There are a variety of ways to specify colors in R, as well as several nice built-in color palletes.
Some resources on color tools in R:
- R color cheatsheet
- colorspace package
- R cookbook colors
- Another nice ggplot color guide
- Colormind
- coolors
- Color Designer
Reminder:
color: The outline color for lines/points/regionsfill: The fill color for regions and point-shapes with a filling (shapes 21-25)
The color scale functions you should use in ggplot all have the following form:
scale_[aesthetic]_[type + pallete]
For example:
Custom Gradients, Color Wheels, and Palettes
- Continuous: scale_color_gradient()
- Continuous: scale_color_gradient2()
- Continuous: scale_color_gradientn()
Binned:
scale_color_steps()Binned:
scale_color_steps2()Binned:
scale_color_stepsn()Discrete:
scale_color_hue()Discrete:
scale_color_manual()
Viridis
- Continuous: scale_color_viridis_c()
- Binned: scale_color_viridis_b()
- Discrete: scale_color_viridis_d()
Color Brewer
- Discrete: scale_color_brewer()
- Continuous: scale_color_distiller()
- Binned: scale_color_fermenter()
Greyscale
- scale_color_grey()
ggthemes
- ggthemes::scale_color_excel()
- ggthemes::economist()
- ggthemes::scale_color_fivethirtyeight()
- ggthemes::scale_color_colorblind()
Other packages - wesanderson - ggsci
58.1 Arguments
All of the arguments that can be used with position scales can also be used with color scales.
namebreakslabelslimitsoobtranspositionguidena.translatena.value
Color-specific arguments
Color scale functions also have arguments to control how the palettes are constructed. The exact arguments vary by scale function. Let’s look at a few.
scale_color_viridis_*()
beginandend
- Values between 0 and 1 saying where on the scale to start and stop.
direction
- Which end to start from?
- 1 or -1
option
- Which of the viridis family palettes to use?
- “A”, “B”, “C”, “D”, “E”
aesthetics
- Which aesthetics to apply the palette to?
- Useful to do
aesthetics = c("color", "fill")
scale_color_brewer()
direction
- Which end to start from?
- 1 or -1
type
- Which kind of color scale to use?
- “seq” (sequential), “div” (diverging), “qual” (qualitative)
palette
- Which specific palette to use?
- Can be a number or one of the named palettes (see
?scale_color_brewer())
aesthetics
scale_color_manual()
For specifying your own discrete palettes.
values
- A character vector of colors names or hex codes to use.
- If the vector has names, it will match data values to names.
- Must be at least as long as the number of unique values.
aesthetics
p <- ggplot(mtcars) +
aes(mpg, wt) +
geom_point(
aes(colour = factor(cyl)),
size = 8
)
p + scale_colour_manual(values = c("4" = "red", "6" = "blue", "8" = "darkgreen"))
pal <- palette.colors("Okabe-Ito", n = 10)
p + scale_colour_manual(values = pal)
pal <- palette.colors("R4", n = 4)
p + scale_colour_manual(values = pal)scale_color_gradient()
For specifying your own gradient.
scale_colour_gradient(
...,
low = "#132B43",
high = "#56B1F7",
na.value = "grey50",
aesthetics = "colour"
)
scale_colour_gradient2(
...,
low = muted("red"),
mid = "white",
high = muted("blue"),
midpoint = 0,
na.value = "grey50",
aesthetics = "colour"
)
scale_colour_gradientn(
...,
colours,
values = NULL,
na.value = "grey50",
aesthetics = "colour",
colors
)scale_color_identity()
A special scale. It doesn’t map colors to a specific value, but uses the variable values directly. Useful if you want to store the color codes directly in the data.