Color

Jeff Stevens

2023-04-07

Source

Data Visualization in R

SDS 375

Claus O. Wilke

Uses of color

Uses of color

  1. Distinguish categories (qualitative)

  2. Represent ordered numeric values (sequential)

  3. Represent binary numeric values (diverging)

  4. Highlight

Distinguish categories

Represent sequential numeric values

Represent diverging numeric values

Highlight

Color space

RGB color space

  • red R (0-255): amount of red

  • green G (0-255): amount of green

  • blue B (0-255): amount of blue

R G B hexcode
0 0 0 #000000
255 0 0 #FF0000
0 255 255 #00FFFF
128 128 128 #808080
0 158 115 #009E73
255 255 255 #FFFFFF

RGB color space

Humans cannot reason well about the RGB color space

HCL color space

Explore HCL colors with colorspace::choose_color()

Choosing colors

Avoid high chroma

High chroma: Toys

Photo by Pixabay from Pexels

Source: Alexas_Fotos

Low chroma: Elegance

Photo by Saviesa Home from Pexels

Source: Saviesa Home

Be aware of color-vision deficiency

5%–8% of men are color blind!

Consider using Okabe-Ito scale

Name Hex code    R, G, B (0-255)
orange #E69F00 230, 159, 0
sky blue #56B4E9 86, 180, 233
bluish green #009E73 0, 158, 115
yellow #F0E442 240, 228, 66
blue #0072B2 0, 114, 178
vermilion #D55E00 213, 94, 0
reddish purple #CC79A7 204, 121, 167
black #000000 0, 0, 0

Consider using viridis

Color scales

Using color scales in ggplot

Scale function Data type Palette type
scale_color_hue()                     discrete         qualitative                                                  
scale_color_gradient() continuous sequential
scale_color_gradient2() continuous diverging
scale_color_brewer() discrete qualitative, diverging, sequential
scale_color_distiller() continuous qualitative, diverging, sequential

Replace color with fill for shaded areas

Color palettes: qualitative

scale_color_brewer()

mpg |> 
  ggplot(aes(x = displ, y = hwy, color = drv)) +
  geom_jitter() +
  labs(x = "Displacement", y = "Highway fuel efficiency") +
  scale_color_brewer(palette = "Accent") +
  theme(legend.position = c(0.8, 0.8))

Color palettes: sequential

scale_color_distiller()

mpg |> 
  ggplot(aes(x = displ, y = hwy, color = cty)) +
  geom_jitter() +
  labs(x = "Displacement", y = "Highway fuel efficiency") +
  scale_color_distiller(palette = "YlGnBu") +
  theme(legend.position = c(0.8, 0.8))

Color palettes: diverging

scale_color_distiller()

mpg |> 
  ggplot(aes(x = displ, y = hwy, color = cty)) +
  geom_jitter() +
  labs(x = "Displacement", y = "Highway fuel efficiency") +
  scale_color_distiller(palette = "Spectral") +
  theme(legend.position = c(0.8, 0.8))

Additional palettes

Additional palettes

Switch to R script

Let’s code!

Color [Rmd]