Elements of Data Science
SDS 322E

H. Sherry Zhang
Department of Statistics and Data Sciences
University of Texas at Austin

Get the repository for today:
library(usethis)
create_from_github("SDS322E-26FALL/0403-viz5", fork = FALSE)

Learning objectives

  • dive deeper into color use in data visualization

    • What’s a good vs. bad color palette? Examples: viridis vs. rainbow
      • perception uniformity
      • colorblindness friendliness
    • Qualitative, Sequential, and diverging palettes
    • A new package to explore: ggthemes

The viridis color palette

Why is viridis a good color palette?

  1. They are perceptually uniform: meaning that values close to each other have similar-appearing colors and values far away from each other have more different-appearing colors, consistently across the range of values.

  2. They are robust to colorblindness, so that the above properties hold true for people with common forms of colorblindness, as well as in grey scale printing.

What about the rainbow palette?

The rainbow palette is not uniformly perceived.

The severity of influenza in Germany in week 8, 2019.

The original color palette (left) is the classic rainbow ranging from “normal” (blue) to “strongly increased” (red).

Color blindness

normal


tritanopia: reduced sensitivity to blue light (extremely rare)

protanopia: reduced sensitivity to red light

deuteranopia: reduced sensitivity to green light (most common)

What does that mean to a color palette?

The rainbow palette

The viridis palette

Color blindness affects about 8% of all males and 0.5% of all females!

What does that mean on the plot?

Normal

Deuteranopia

Protanopia

Tritanopia

The rainbow color palette is also not color blind-friendly because baseline color (blue) gets emphasized with deuteranopia and protanopia.

What does that mean on the plot?

normal

tritanopia

protanopia

deuteranopia (most common)

Good color platettes go beyond viridis

Three types of color schemes designed for different types of data:

  • Qualitative: for categorical information, i.e., where no particular ordering of categories is available and every color should receive the same perceptual weight.

  • Sequential: for ordered/numeric information, i.e., going from high to low (or vice versa).

  • Diverging: for ordered/numeric information around a central neutral value, i.e., where colors diverge from neutral to two extremes.

Qualitative color palette: Dark2

The Dark 2 palette from RColorBrewer

ggplot(mtcars, aes(x = mpg, y = disp)) + 
  geom_point(aes(color = as.factor(cyl)), size = 5) + 
  scale_color_brewer(palette = "Dark2",
                     name = "cylinder")

Qualitative color palette: Okabe-Ito

New package: ggthemes has many cute themes, scales, and geometries that worth checking out.

ggplot(mtcars, aes(x = mpg, y = disp)) + 
  geom_point(aes(color = as.factor(cyl)), size = 5) + 
  ggthemes::scale_color_colorblind(
    name = "cylinder"
    ) 

To use ggthemes, you need to install it first using install.packages("ggthemes") in the console, and then load it using library(ggthemes) in the script.

Your time

This is a plot I show you in week1 hello-world.pdf. Can you use the mtcars data with things you’ve learnt from ggplot2 to create the exact same plot?

There are some hints in the next slides to guide you make this plot step-by-step.

Your time

  1. Base plot: Start with a base plot that map the variables in mtcars to the x, y-axis, color, and facet.

  2. Color: The color seems to be mapped to a continuous value. Is it the best choice? How would you change it? What’s the scale_xxx_xxx() function to change to a different color palette.

  3. Facet: The facet header (0 and 1) are not informative, how would you change it. Maybe we can recode 0 and 1 to its actual meaning. How would you do that?

  4. Labels: Use a more informative x and y axis title, and legend name

  5. Theme: Play around with theme and arrange the legend position to bottom