+ - 0:00:00
Notes for current slide
Notes for next slide

Color scales

Claus O. Wilke

last updated: 2024-02-05

Uses of color in data visualization

Uses of color in data visualization

1. Distinguish categories (qualitative)

Qualitative scale example

Palette name: Okabe-Ito

Qualitative scale example

Palette name: ColorBrewer Set1

Qualitative scale example

Palette name: ColorBrewer Set3

Uses of color in data visualization

1. Distinguish categories (qualitative)
2. Represent numeric values (sequential)

Sequential scale example


Palette name: Viridis

Sequential scale example


Palette name: Inferno

Sequential scale example


Palette name: Cividis

Uses of color in data visualization

1. Distinguish categories (qualitative)
2. Represent numeric values (sequential)
3. Represent numeric values (diverging)

Diverging scale example

Palette name: ColorBrewer PiYG

Diverging scale example

Palette name: Carto Earth

Diverging scale example

Palette name: Blue-Red

Uses of color in data visualization

1. Distinguish categories (qualitative)
2. Represent numeric values (sequential)
3. Represent numeric values (diverging)
4. Highlight

Highlight example

Palette name: Grays with accents

Highlight example

Palette name: Okabe-Ito accent

Highlight example

Palette name: ColorBrewer accent

Uses of color in data visualization

1. Distinguish categories (qualitative)
2. Represent numeric values (sequential)
3. Represent numeric values (diverging)
4. Highlight

Color scales in ggplot2

Getting the data

The temps_months dataset:

temps_months <- read_csv("https://wilkelab.org/SDS375/datasets/tempnormals.csv") %>%
group_by(location, month_name) %>%
summarize(mean = mean(temperature)) %>%
mutate(
month = factor(
month_name,
levels = c("Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
),
location = factor(
location, levels = c("Death Valley", "Houston", "San Diego", "Chicago")
)
) %>%
select(-month_name)

Getting the data

The popgrowth dataset:

US_census <- read_csv("https://wilkelab.org/SDS375/datasets/US_census.csv")
US_regions <- read_csv("https://wilkelab.org/SDS375/datasets/US_regions.csv")
popgrowth <- left_join(US_census, US_regions) %>%
group_by(region, division, state) %>%
summarize(
pop2000 = sum(pop2000, na.rm = TRUE),
pop2010 = sum(pop2010, na.rm = TRUE),
popgrowth = (pop2010-pop2000)/pop2000,
.groups = "drop"
) %>%
mutate(region = factor(region, levels = c("West", "South", "Midwest", "Northeast")))

ggplot2 color scale functions are a bit of a mess

ggplot2 color scale functions are a bit of a mess

Scale function Aesthetic     Data type Palette type
scale_color_hue()                     color discrete         qualitative                                                  

ggplot2 color scale functions are a bit of a mess

Scale function Aesthetic     Data type Palette type
scale_color_hue()                     color discrete         qualitative                                                  
scale_fill_hue() fill discrete qualitative

ggplot2 color scale functions are a bit of a mess

Scale function Aesthetic     Data type Palette type
scale_color_hue()                     color discrete         qualitative                                                  
scale_fill_hue() fill discrete qualitative
scale_color_gradient() color continuous sequential

ggplot2 color scale functions are a bit of a mess

Scale function Aesthetic     Data type Palette type
scale_color_hue()                     color discrete         qualitative                                                  
scale_fill_hue() fill discrete qualitative
scale_color_gradient() color continuous sequential
scale_color_gradient2() color continuous diverging

ggplot2 color scale functions are a bit of a mess

Scale function Aesthetic     Data type Palette type
scale_color_hue()                     color discrete         qualitative                                                  
scale_fill_hue() fill discrete qualitative
scale_color_gradient() color continuous sequential
scale_color_gradient2() color continuous diverging
scale_fill_viridis_c() color continuous sequential
scale_fill_viridis_d() fill discrete sequential
scale_color_brewer() color discrete qualitative, diverging, sequential
scale_fill_brewer() fill discrete qualitative, diverging, sequential
scale_color_distiller() color continuous qualitative, diverging, sequential

... and there are many many more

Examples

ggplot(temps_months, aes(x = month, y = location, fill = mean)) +
geom_tile(width = 0.95, height = 0.95) +
coord_fixed(expand = FALSE) +
theme_classic()
# no fill scale defined, default is scale_fill_gradient()

Examples

ggplot(temps_months, aes(x = month, y = location, fill = mean)) +
geom_tile(width = 0.95, height = 0.95) +
coord_fixed(expand = FALSE) +
theme_classic() +
scale_fill_gradient()

Examples

ggplot(temps_months, aes(x = month, y = location, fill = mean)) +
geom_tile(width = 0.95, height = 0.95) +
coord_fixed(expand = FALSE) +
theme_classic() +
scale_fill_viridis_c()

Examples

ggplot(temps_months, aes(x = month, y = location, fill = mean)) +
geom_tile(width = 0.95, height = 0.95) +
coord_fixed(expand = FALSE) +
theme_classic() +
scale_fill_viridis_c(option = "B", begin = 0.15)

Examples

ggplot(temps_months, aes(x = month, y = location, fill = mean)) +
geom_tile(width = 0.95, height = 0.95) +
coord_fixed(expand = FALSE) +
theme_classic() +
scale_fill_distiller(palette = "YlGnBu")

The colorspace package creates some order

Scale name: scale_<aesthetic>_<datatype>_<colorscale>()

The colorspace package creates some order

Scale name: scale_<aesthetic>_<datatype>_<colorscale>()

  • <aesthetic>: name of the aesthetic (fill, color, colour)
  • <datatype>: type of variable plotted (discrete, continuous, binned)
  • <colorscale>: type of the color scale (qualitative, sequential, diverging, divergingx)

The colorspace package creates some order

Scale name: scale_<aesthetic>_<datatype>_<colorscale>()

  • <aesthetic>: name of the aesthetic (fill, color, colour)
  • <datatype>: type of variable plotted (discrete, continuous, binned)
  • <colorscale>: type of the color scale (qualitative, sequential, diverging, divergingx)
Scale function Aesthetic     Data type Palette type    
scale_color_discrete_qualitative() color discrete qualitative
scale_fill_continuous_sequential() fill continuous sequential
scale_colour_continous_divergingx() colour continuous diverging

Examples

ggplot(temps_months, aes(x = month, y = location, fill = mean)) +
geom_tile(width = 0.95, height = 0.95) +
coord_fixed(expand = FALSE) +
theme_classic() +
scale_fill_continuous_sequential(palette = "YlGnBu", rev = FALSE)

Examples

ggplot(temps_months, aes(x = month, y = location, fill = mean)) +
geom_tile(width = 0.95, height = 0.95) +
coord_fixed(expand = FALSE) +
theme_classic() +
scale_fill_continuous_sequential(palette = "Viridis", rev = FALSE)

Examples

ggplot(temps_months, aes(x = month, y = location, fill = mean)) +
geom_tile(width = 0.95, height = 0.95) +
coord_fixed(expand = FALSE) +
theme_classic() +
scale_fill_continuous_sequential(palette = "Inferno", begin = 0.15, rev = FALSE)

colorspace::hcl_palettes(type = "sequential", plot = TRUE) # all sequential palettes

colorspace::hcl_palettes(type = "diverging", plot = TRUE, n = 9) # all diverging palettes

colorspace::divergingx_palettes(plot = TRUE, n = 9) # all divergingx palettes

Setting colors manually for discrete, qualitative scales

Discrete, qualitative scales are best set manually

ggplot(popgrowth, aes(x = pop2000, y = popgrowth, color = region)) +
geom_point() +
scale_x_log10()
# no color scale defined, default is scale_color_hue()

Discrete, qualitative scales are best set manually

ggplot(popgrowth, aes(x = pop2000, y = popgrowth, color = region)) +
geom_point() +
scale_x_log10() +
scale_color_hue()

Discrete, qualitative scales are best set manually

library(ggthemes) # for scale_color_colorblind()
ggplot(popgrowth, aes(x = pop2000, y = popgrowth, color = region)) +
geom_point() +
scale_x_log10() +
scale_color_colorblind() # uses Okabe-Ito colors

Discrete, qualitative scales are best set manually

ggplot(popgrowth, aes(x = pop2000, y = popgrowth, color = region)) +
geom_point() +
scale_x_log10() +
scale_color_manual(
values = c(West = "#E69F00", South = "#56B4E9", Midwest = "#009E73", Northeast = "#F0E442")
)

Okabe-Ito RGB codes

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

Further reading

Uses of color in data visualization

Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow