class: center, middle, inverse, title-slide # Data visualization ## Color and Themes ### Ben Baumer ### SDS 192Feb 7, 2020(
http://beanumber.github.io/sds192/lectures/mdsr_viz_05-color.html
) --- class: center, middle, inverse # Color --- ## Three color schemes .pull-left[ - Sequential - Single hue - Multi-hue - Diverging - Categorical ] .pull-right[ ![](https://media.giphy.com/media/3oriO1VZF0t5PyQVlC/giphy.gif) ] -- Big Idea: color **choices matter** and there are better and worse choices --- ## Sequential, single hue .pull-left[ ![](figures/cali_single_hue-1.png)<!-- --> ] .pull-right[ - Map [**saturation**](https://en.wikipedia.org/wiki/Colorfulness) to numeric variable - No more than 5--6 levels - People interpret high saturation as more ] --- ## Sequential, single hue .pull-left[ ![](figures/cali_single_hue_discrete-1.png)<!-- --> ] .pull-right[ - Map [**saturation**](https://en.wikipedia.org/wiki/Colorfulness) to numeric variable - **No more than 5--6 levels** - People interpret high saturation as more ] --- ## Sequential, double hue .pull-left[ - Map [**hue**](https://en.wikipedia.org/wiki/Hue) to numeric variable - Generally, no more than 2 anchor colors - People interpret darker hue as more ] .pull-right[ ![](figures/cali-multihue-1.png)<!-- --> ] --- ## Diverging .pull-left[ ![](figures/cali-diverging-1.png)<!-- --> ] .pull-right[ - Two colors used to indicate extremes of a range - Neutral color in the middle - distinguish between *zero* and *missing* ] --- ## Categorical .pull-left[ - Different color = different category - choose colors that are - perceptually distant - roughly the same saturation ] .pull-right[ ![](figures/cali-categorical-1.png)<!-- --> ] --- ## Built-in color palettes - Cynthia Brewer .footnote[http://www.colorbrewer2.org] - `RColorBrewer` package - `scale_color_brewer()` function - [`viridis`](https://cran.r-project.org/web/packages/viridis/vignettes/intro-to-viridis.html) palettes mimic `matplotlib` Python library --- ## Sorry, no rainbows .pull-left[ ![](https://media.giphy.com/media/3o7aD0Meng5BT8IejK/giphy.gif) ] .pull-right[ - ❌ 🌈 NO RAINBOWS!! - [Why rainbow colors aren’t the best option for data visualizations](https://www.poynter.org/archive/2013/why-rainbow-colors-arent-always-the-best-options-for-data-visualizations/) - [How The Rainbow Color Map Misleads](https://eagereyes.org/basics/rainbow-color-map) ] .footnote[https://policyviz.com/2018/08/07/dataviz-cheatsheet/] --- ## `ggplot2` default color ```r library(tidyverse) rand_data <- data.frame(x = rnorm(10000), y = rnorm(10000)) norm_plot <- ggplot(rand_data, aes(x = x, y = y)) + geom_hex() + coord_fixed() norm_plot ``` ![](figures/norm_plot-1.png)<!-- --> --- ## ColorBrewer `Purples` palette ```r norm_plot + scale_fill_distiller(palette = "Purples") ``` ![](figures/norm_plot_distiller-1.png)<!-- --> --- ## ColorBrewer `YlGn` palette ```r norm_plot + scale_fill_distiller(palette = "YlGn") ``` ![](figures/norm_plot_distiller2-1.png)<!-- --> --- ## Viridis palette ```r norm_plot + scale_fill_viridis_c() ``` ![](figures/norm_plot_viridis-1.png)<!-- --> --- class: center, middle, inverse # Themes --- ## Take any ggplot... ```r library(tidyverse) p1 <- ggplot(mtcars, aes(x = disp, y = mpg, color = factor(cyl))) + geom_point() p1 ``` ![](figures/gg1-1.png)<!-- --> --- ## ...and change themes with `ggthemes` .footnote[https://github.com/jrnold/ggthemes] ```r library(ggthemes) p1 + theme_wsj() ``` ![](figures/gg-theme-1.png)<!-- -->