class: center, middle, title-slide .title[ # Color scales ] .author[ ### Claus O. Wilke, remixed by Joseph Elsherbini ] .date[ ### 26-08-2022 ] --- # Uses of color in data visualization -- <table style = "border: none; line-height: 2.5;"> <tr style = "background: white;"> <td style = "text-align: left; width: 50%;"> 1. Distinguish categories (qualitative) </td> <td> <img src = "color-scales_files/qualitative.png" width = 100% style = "text-align: right; vertical-align: middle"></img> </td> </tr> </table> --- # Qualitative scale example .center.nogap[ <!-- --> ] Humans can only distinguish 7 or 8 colors. ??? Figure redrawn from [Claus O. Wilke. Fundamentals of Data Visualization. O'Reilly, 2019.](https://clauswilke.com/dataviz) --- # Qualitative scale example .center.nogap[ <!-- --> ] Palette name: Okabe-Ito ??? Figure redrawn from [Claus O. Wilke. Fundamentals of Data Visualization. O'Reilly, 2019.](https://clauswilke.com/dataviz) --- # Qualitative scale example .center.nogap[ <!-- --> ] Palette name: ColorBrewer Set1 ??? Figure redrawn from [Claus O. Wilke. Fundamentals of Data Visualization. O'Reilly, 2019.](https://clauswilke.com/dataviz) --- # Qualitative scale example .center.nogap[ <!-- --> ] Palette name: ColorBrewer Set3 ??? Figure redrawn from [Claus O. Wilke. Fundamentals of Data Visualization. O'Reilly, 2019.](https://clauswilke.com/dataviz) --- # Uses of color in data visualization <table style = "border: none; line-height: 2.5;"> <tr style = "background: white;"> <td style = "text-align: left; width: 50%;"> 1. Distinguish categories (qualitative) </td> <td> <img src = "color-scales_files/qualitative.png" width = 100% style = "text-align: right; vertical-align: middle"></img> </td> </tr> <tr style = "background: white;"> <td style = "text-align: left;"> 2. Represent numeric values (sequential) </td> <td> <img src = "color-scales_files/sequential.png" width = 100% style = "text-align: right; vertical-align: middle"></img> </td> </tr> </table> --- # Sequential scale example <br> .center[ <!-- --> ] Palette name: Viridis ??? Figure redrawn from [Claus O. Wilke. Fundamentals of Data Visualization. O'Reilly, 2019.](https://clauswilke.com/dataviz) --- # Sequential scale example <br> .center[ <!-- --> ] Palette name: Inferno ??? Figure redrawn from [Claus O. Wilke. Fundamentals of Data Visualization. O'Reilly, 2019.](https://clauswilke.com/dataviz) --- # Sequential scale example <br> .center[ <!-- --> ] Palette name: Cividis ??? Figure redrawn from [Claus O. Wilke. Fundamentals of Data Visualization. O'Reilly, 2019.](https://clauswilke.com/dataviz) --- # Uses of color in data visualization <table style = "border: none; line-height: 2.5;"> <tr style = "background: white;"> <td style = "text-align: left; width: 50%;"> 1. Distinguish categories (qualitative) </td> <td> <img src = "color-scales_files/qualitative.png" width = 100% style = "text-align: right; vertical-align: middle"></img> </td> </tr> <tr style = "background: white;"> <td style = "text-align: left;"> 2. Represent numeric values (sequential) </td> <td> <img src = "color-scales_files/sequential.png" width = 100% style = "text-align: right; vertical-align: middle"></img> </td> </tr> <tr style = "background: white;"> <td style = "text-align: left;"> 3. Represent numeric values (diverging) </td> <td> <img src = "color-scales_files/diverging.png" width = 100% style = "text-align: right; vertical-align: middle"></img> </td> </tr> </table> --- # Diverging scale example .center.nogap[ ``` Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0. ℹ Please use `linewidth` instead. ``` <!-- --> ] Palette name: ColorBrewer PiYG ??? Figure redrawn from [Claus O. Wilke. Fundamentals of Data Visualization. O'Reilly, 2019.](https://clauswilke.com/dataviz) --- # Diverging scale example .center.nogap[ <!-- --> ] Palette name: Carto Earth ??? Figure redrawn from [Claus O. Wilke. Fundamentals of Data Visualization. O'Reilly, 2019.](https://clauswilke.com/dataviz) --- # Diverging scale example .center.nogap[ <!-- --> ] Palette name: Blue-Red ??? Figure redrawn from [Claus O. Wilke. Fundamentals of Data Visualization. O'Reilly, 2019.](https://clauswilke.com/dataviz) --- # Uses of color in data visualization <table style = "border: none; line-height: 2.5;"> <tr style = "background: white;"> <td style = "text-align: left; width: 50%;"> 1. Distinguish categories (qualitative) </td> <td> <img src = "color-scales_files/qualitative.png" width = 100% style = "text-align: right; vertical-align: middle;"></img> </td> </tr> <tr style = "background: white;"> <td style = "text-align: left;"> 2. Represent numeric values (sequential) </td> <td> <img src = "color-scales_files/sequential.png" width = 100% style = "text-align: right; vertical-align: middle"></img> </td> </tr> <tr style = "background: white;"> <td style = "text-align: left;"> 3. Represent numeric values (diverging) </td> <td> <img src = "color-scales_files/diverging.png" width = 100% style = "text-align: right; vertical-align: middle"></img> </td> </tr> <tr style = "background: white;"> <td style = "text-align: left;"> 4. Highlight </td> <td> <img src = "color-scales_files/highlight.png" width = 100% style = "text-align: right; vertical-align: middle"></img> </td> </tr> </table> --- # Highlight example .center[ <!-- --> ] Palette name: Grays with accents ??? Figure redrawn from [Claus O. Wilke. Fundamentals of Data Visualization. O'Reilly, 2019.](https://clauswilke.com/dataviz) --- # Highlight example .center[ <!-- --> ] Palette name: Okabe-Ito accent ??? Figure redrawn from [Claus O. Wilke. Fundamentals of Data Visualization. O'Reilly, 2019.](https://clauswilke.com/dataviz) --- # Highlight example .center[ <!-- --> ] Palette name: ColorBrewer accent ??? Figure redrawn from [Claus O. Wilke. Fundamentals of Data Visualization. O'Reilly, 2019.](https://clauswilke.com/dataviz) --- # Uses of color in data visualization <table style = "border: none; line-height: 2.5;"> <tr style = "background: white;"> <td style = "text-align: left; width: 50%;"> 1. Distinguish categories (qualitative) </td> <td> <img src = "color-scales_files/qualitative.png" width = 100% style = "text-align: right; vertical-align: middle;"></img> </td> </tr> <tr style = "background: white;"> <td style = "text-align: left;"> 2. Represent numeric values (sequential) </td> <td> <img src = "color-scales_files/sequential.png" width = 100% style = "text-align: right; vertical-align: middle"></img> </td> </tr> <tr style = "background: white;"> <td style = "text-align: left;"> 3. Represent numeric values (diverging) </td> <td> <img src = "color-scales_files/diverging.png" width = 100% style = "text-align: right; vertical-align: middle"></img> </td> </tr> <tr style = "background: white;"> <td style = "text-align: left;"> 4. Highlight </td> <td> <img src = "color-scales_files/highlight.png" width = 100% style = "text-align: right; vertical-align: middle"></img> </td> </tr> </table> [//]: # "segment ends here" --- class: center middle ## Color scales in **ggplot2** --- ## **ggplot2** color scale functions are a bit of a mess -- .small-font.center[ Scale function | Aesthetic | Data type | Palette type :----------- | :---------- | :------------ | :------------ `scale_color_hue()` | `color` | discrete | qualitative ] --- ## **ggplot2** color scale functions are a bit of a mess .small-font.center[ 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 .small-font.center[ 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 .small-font.center[ 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 .small-font.center[ 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 .small-font[ ```r 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() ``` ] .center[ <!-- --> ] --- ## Examples .small-font[ ```r 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() ``` ] .center[ <!-- --> ] --- ## Examples .small-font[ ```r 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() ``` ] .center[ <!-- --> ] --- ## Examples .small-font[ ```r 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) ``` ] .center[ <!-- --> ] --- ## Examples .small-font[ ```r 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") ``` ] .center[ <!-- --> ] --- ## The **colorspace** package creates some order Scale name: `scale_<aesthetic>_<datatype>_<colorscale>()` -- .small-font[ - `<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`) ] -- .small-font.center[ 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 .tiny-font[ ```r 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) ``` ] .center[ <!-- --> ] --- ## Examples .tiny-font[ ```r 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) ``` ] .center[ <!-- --> ] --- ## Examples .tiny-font[ ```r 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) ``` ] .center[ <!-- --> ] --- .tiny-font.nogap[ ```r colorspace::hcl_palettes(type = "sequential", plot = TRUE) # all sequential palettes ``` .center[ <!-- --> ] ] --- .tiny-font.nogap[ ```r colorspace::hcl_palettes(type = "diverging", plot = TRUE, n = 9) # all diverging palettes ``` .center[ <!-- --> ] ] --- .tiny-font.nogap[ ```r colorspace::divergingx_palettes(plot = TRUE, n = 9) # all divergingx palettes ``` .center[ <!-- --> ] ] [//]: # "segment ends here" --- class: center middle ## Setting colors manually for discrete, qualitative scales --- ## Discrete, qualitative scales are best set manually .tiny-font[ ```r ggplot(popgrowth, aes(x = pop2000, y = popgrowth, color = region)) + geom_point() + scale_x_log10() # no color scale defined, default is scale_color_hue() ``` ] .center[ <!-- --> ] --- ## Discrete, qualitative scales are best set manually .tiny-font[ ```r ggplot(popgrowth, aes(x = pop2000, y = popgrowth, color = region)) + geom_point() + scale_x_log10() + scale_color_hue() ``` ] .center[ <!-- --> ] --- ## Discrete, qualitative scales are best set manually .tiny-font[ ```r 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 ``` ] .center[ <!-- --> ] --- ## Discrete, qualitative scales are best set manually .tiny-font[ ```r 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") ) ``` ] .center[ <!-- --> ] --- ## Okabe-Ito RGB codes .center[ <img src = "https://clauswilke.com/dataviz/pitfalls_of_color_use_files/figure-html/palette-Okabe-Ito-1.png", width = 100%></img> ] .tiny-font[ 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 ] ??? Figure from [Claus O. Wilke. Fundamentals of Data Visualization. O'Reilly, 2019.](https://clauswilke.com/dataviz) [//]: # "segment ends here" --- ## Be aware of color-vision deficiency 5%–8% of men are color blind! -- <img src = "https://clauswilke.com/dataviz/pitfalls_of_color_use_files/figure-html/red-green-cvd-sim-1.png", width = 100%></img> Red-green color-vision deficiency is the most common --- ## Be aware of color-vision deficiency 5%–8% of men are color blind! <img src = "https://clauswilke.com/dataviz/pitfalls_of_color_use_files/figure-html/blue-green-cvd-sim-1.png", width = 100%></img> Blue-green color-vision deficiency is rare but does occur --- ## Be aware of color-vision deficiency Choose colors that can be distinguished with CVD <img src = "https://clauswilke.com/dataviz/pitfalls_of_color_use_files/figure-html/PiYG-cvd-sim-1.png", width = 100%></img> --- ## Consider using the Okabe-Ito scale as your default .center[ <img src = "https://clauswilke.com/dataviz/pitfalls_of_color_use_files/figure-html/palette-Okabe-Ito-1.png", width = 100%></img> ] .tiny-font[ 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 ] ??? Figure from [Claus O. Wilke. Fundamentals of Data Visualization. O'Reilly, 2019.](https://clauswilke.com/dataviz) --- ## CVD is worse for thin lines and tiny dots .move-up-1em.center[ <img src = "https://clauswilke.com/dataviz/pitfalls_of_color_use_files/figure-html/colors-thin-lines-1.png", width = 65%></img> ] --- ## When in doubt, run CVD simulations <div class = "move-up-1em" style = "width: 50%; box-shadow: 3px 5px 3px 1px #00000080; padding: 8px;"> .small-font[Original]<br> <img src = "https://clauswilke.com/dataviz/redundant_coding_files/figure-html/iris-scatter-three-shapes-1.png", width = 100%></img> </div> ??? Figure from [Claus O. Wilke. Fundamentals of Data Visualization. O'Reilly, 2019.](https://clauswilke.com/dataviz) --- ## When in doubt, run CVD simulations <div class = "move-up-1em" style = "width: 50%; box-shadow: 3px 5px 3px 1px #00000080; padding: 8px;"> .small-font[Original]<br> <img src = "https://clauswilke.com/dataviz/redundant_coding_files/figure-html/iris-scatter-three-shapes-1.png", width = 100%></img> </div> <img src = "https://clauswilke.com/dataviz/redundant_coding_files/figure-html/iris-scatter-three-shapes-cvd-1.png", width = 60% style = "position: absolute; top: 22%; left: 30%; box-shadow: 3px 5px 3px 1px #00000080; padding: 16px 8px 8px 16px; background: white"></img> ??? Figures from [Claus O. Wilke. Fundamentals of Data Visualization. O'Reilly, 2019.](https://clauswilke.com/dataviz) [//]: # "segment ends here" -- the **colorspace** package can help --- # Exercise Go to [https://elsherbini.github.io/AMNH_R_Workshop_2023/modules/data-wrangling-module/](https://elsherbini.github.io/AMNH_R_Workshop_2023/modules/data-wrangling-module/) and complete the color scales exercise.