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:

  1. R color cheatsheet
  2. colorspace package
  3. R cookbook colors
  4. Another nice ggplot color guide
  5. Colormind
  6. coolors
  7. Color Designer

Reminder:

  • color: The outline color for lines/points/regions
  • fill: 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.

  1. name
  2. breaks
  3. labels
  4. limits
  5. oob
  6. trans
  7. position
  8. guide
  9. na.translate
  10. na.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_*()

  1. begin and end
  • Values between 0 and 1 saying where on the scale to start and stop.
  1. direction
  • Which end to start from?
  • 1 or -1
  1. option
  • Which of the viridis family palettes to use?
  • “A”, “B”, “C”, “D”, “E”
  1. aesthetics
  • Which aesthetics to apply the palette to?
  • Useful to do aesthetics = c("color", "fill")

scale_color_brewer()

  1. direction
  • Which end to start from?
  • 1 or -1
  1. type
  • Which kind of color scale to use?
  • “seq” (sequential), “div” (diverging), “qual” (qualitative)
  1. palette
  • Which specific palette to use?
  • Can be a number or one of the named palettes (see ?scale_color_brewer())
  1. aesthetics

scale_color_manual()

For specifying your own discrete palettes.

  1. 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.
  1. 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.