class: center, middle, title-slide .title[ # Aesthetic mappings ] .author[ ### Claus O. Wilke ] .date[ ### last updated: 2024-01-29 ] --- class: center middle ## Plots map data onto graphical elements --- ## Dataset:<br>Daily average temperatures for various locations .small-font[.center[ <table> <thead> <tr> <th style="text-align:left;"> location </th> <th style="text-align:right;"> day_of_year </th> <th style="text-align:left;"> month </th> <th style="text-align:right;"> temperature </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Death Valley </td> <td style="text-align:right;"> 1 </td> <td style="text-align:left;"> 01 </td> <td style="text-align:right;"> 51.0 </td> </tr> <tr> <td style="text-align:left;"> Death Valley </td> <td style="text-align:right;"> 2 </td> <td style="text-align:left;"> 01 </td> <td style="text-align:right;"> 51.2 </td> </tr> <tr> <td style="text-align:left;"> Death Valley </td> <td style="text-align:right;"> 3 </td> <td style="text-align:left;"> 01 </td> <td style="text-align:right;"> 51.3 </td> </tr> <tr> <td style="text-align:left;"> Death Valley </td> <td style="text-align:right;"> 4 </td> <td style="text-align:left;"> 01 </td> <td style="text-align:right;"> 51.4 </td> </tr> <tr> <td style="text-align:left;"> Death Valley </td> <td style="text-align:right;"> 5 </td> <td style="text-align:left;"> 01 </td> <td style="text-align:right;"> 51.6 </td> </tr> <tr> <td style="text-align:left;"> Death Valley </td> <td style="text-align:right;"> 6 </td> <td style="text-align:left;"> 01 </td> <td style="text-align:right;"> 51.7 </td> </tr> <tr> <td style="text-align:left;"> Death Valley </td> <td style="text-align:right;"> 7 </td> <td style="text-align:left;"> 01 </td> <td style="text-align:right;"> 51.9 </td> </tr> <tr> <td style="text-align:left;"> Death Valley </td> <td style="text-align:right;"> 8 </td> <td style="text-align:left;"> 01 </td> <td style="text-align:right;"> 52.0 </td> </tr> <tr> <td style="text-align:left;"> Death Valley </td> <td style="text-align:right;"> 9 </td> <td style="text-align:left;"> 01 </td> <td style="text-align:right;"> 52.2 </td> </tr> <tr> <td style="text-align:left;"> Death Valley </td> <td style="text-align:right;"> 10 </td> <td style="text-align:left;"> 01 </td> <td style="text-align:right;"> 52.3 </td> </tr> <tr> <td style="text-align:left;"> Death Valley </td> <td style="text-align:right;"> 11 </td> <td style="text-align:left;"> 01 </td> <td style="text-align:right;"> 52.5 </td> </tr> <tr> <td style="text-align:left;"> Death Valley </td> <td style="text-align:right;"> 12 </td> <td style="text-align:left;"> 01 </td> <td style="text-align:right;"> 52.7 </td> </tr> <tr> <td style="text-align:left;"> Death Valley </td> <td style="text-align:right;"> 13 </td> <td style="text-align:left;"> 01 </td> <td style="text-align:right;"> 52.9 </td> </tr> <tr> <td style="text-align:left;"> Death Valley </td> <td style="text-align:right;"> 14 </td> <td style="text-align:left;"> 01 </td> <td style="text-align:right;"> 53.0 </td> </tr> <tr> <td style="text-align:left;"> Death Valley </td> <td style="text-align:right;"> 15 </td> <td style="text-align:left;"> 01 </td> <td style="text-align:right;"> 53.2 </td> </tr> </tbody> </table> ]] --- ## Temperatures mapped onto y position .center[ <img src="aesthetic-mappings_files/figure-html/temp-normals-vs-time-1.svg" width="70%" /> ] ??? Figure redrawn from [Claus O. Wilke. Fundamentals of Data Visualization. O'Reilly, 2019.](https://clauswilke.com/dataviz) --- ## Temperatures mapped onto color <br> .center[ <img src="aesthetic-mappings_files/figure-html/four-locations-temps-by-month-1.svg" width="85%" /> ] ??? Figure redrawn from [Claus O. Wilke. Fundamentals of Data Visualization. O'Reilly, 2019.](https://clauswilke.com/dataviz) --- ## Commonly used aesthetics .center[ <img src = "https://clauswilke.com/dataviz/aesthetic_mapping_files/figure-html/common-aesthetics-1.png", width = 90%></img> ] ??? Figure from [Claus O. Wilke. Fundamentals of Data Visualization. O'Reilly, 2019.](https://clauswilke.com/dataviz) --- ## The same data values can be mapped to different aesthetics .center[ <img src = "https://clauswilke.com/dataviz/aesthetic_mapping_files/figure-html/basic-scales-example-1.png", width = 90%></img> ] ??? Figure from [Claus O. Wilke. Fundamentals of Data Visualization. O'Reilly, 2019.](https://clauswilke.com/dataviz) --- ## We can use many different aesthetics at once <br> .center.move-up-1em[ <img src="aesthetic-mappings_files/figure-html/mtcars-five-scale-1.svg" width="60%" /> ] ??? Figure from [Claus O. Wilke. Fundamentals of Data Visualization. O'Reilly, 2019.](https://clauswilke.com/dataviz) [//]: # "segment ends here" --- class: center middle ## Creating aesthetic mappings in ggplot --- ## Getting the data All examples will use the `temperatures` dataset: .tiny-font[ ```r temperatures <- read_csv("https://wilkelab.org/SDS375/datasets/tempnormals.csv") %>% mutate( location = factor( location, levels = c("Death Valley", "Houston", "San Diego", "Chicago") ) ) %>% select(location, station_id, day_of_year, month, temperature) ``` ] --- ## We define the mapping with `aes()` .small-font[ ```r ggplot( data = temperatures, mapping = aes(x = day_of_year, y = temperature, color = location) ) + geom_line() ``` ] .center[ <img src="aesthetic-mappings_files/figure-html/ggplot-temps-example-out-1.svg" width="55%" /> ] --- ## We define the mapping with `aes()` .small-font[ ```r ggplot( data = temperatures, mapping = aes(x = day_of_year, y = location, color = temperature) ) + geom_point(size = 5) ``` ] .center[ <img src="aesthetic-mappings_files/figure-html/ggplot-temps-example2-out-1.svg" width="75%" /> ] --- ## We frequently omit argument names Long form, all arguments are named: .small-font[ ```r ggplot( data = temperatures, mapping = aes(x = day_of_year, y = location, color = temperature) ) + geom_point(size = 5) ``` ] -- Abbreviated form, common arguments remain unnamed: .small-font[ ```r ggplot(temperatures, aes(day_of_year, location, color = temperature)) + geom_point(size = 5) ``` ] --- ## The geom determines how the data is shown .small-font[ ```r ggplot(temperatures, aes(day_of_year, temperature, color = location)) + geom_line() ``` ] .center[ <img src="aesthetic-mappings_files/figure-html/ggplot-temps-example5-out-1.svg" width="55%" /> ] --- ## The geom determines how the data is shown .small-font[ ```r ggplot(temperatures, aes(day_of_year, location, color = temperature)) + geom_point(size = 5) ``` ] .center[ <img src="aesthetic-mappings_files/figure-html/ggplot-temps-example6-out-1.svg" width="75%" /> ] --- ## The geom determines how the data is shown .small-font[ ```r ggplot(temperatures, aes(month, temperature, color = location)) + geom_boxplot() ``` ] .center[ <img src="aesthetic-mappings_files/figure-html/ggplot-temps-example7-out-1.svg" width="80%" /> ] --- ## The geom determines how the data is shown .small-font[ ```r ggplot(temperatures, aes(month, temperature, fill = location)) + geom_violin() + facet_wrap(vars(location)) # make separate panel per location ``` ] .center[ <img src="aesthetic-mappings_files/figure-html/ggplot-temps-example8-out-1.svg" width="90%" /> ] [//]: # "segment ends here" --- class: center middle ## Important: `color` and `fill` apply to different elements --- ## `color` and `fill` apply to different elements `color`<br> Applies color to points, lines, text, borders -- `fill`<br> Applies color to any filled areas --- ## Many geoms have both `color` and `fill` aesthetics .tiny-font[ ```r ggplot(temperatures, aes(month, temperature, color = location)) + geom_boxplot() ``` ] .center[ <img src="aesthetic-mappings_files/figure-html/ggplot-color-fill-out-1.svg" width="80%" /> ] --- ## Many geoms have both `color` and `fill` aesthetics .tiny-font[ ```r ggplot(temperatures, aes(month, temperature, fill = location)) + geom_boxplot() ``` ] .center[ <img src="aesthetic-mappings_files/figure-html/ggplot-color-fill2-out-1.svg" width="80%" /> ] --- ## Many geoms have both `color` and `fill` aesthetics .tiny-font[ ```r ggplot(temperatures, aes(month, temperature, color = location, fill = location)) + geom_boxplot() ``` ] .center[ <img src="aesthetic-mappings_files/figure-html/ggplot-color-fill3-out-1.svg" width="80%" /> ] --- ## Aesthetics can also be used as parameters in geoms .tiny-font[ ```r ggplot(temperatures, aes(month, temperature, fill = location)) + geom_boxplot(color = "steelblue") ``` ] .center[ <img src="aesthetic-mappings_files/figure-html/ggplot-color-fill-params-out-1.svg" width="80%" /> ] --- ## Aesthetics can also be used as parameters in geoms .tiny-font[ ```r ggplot(temperatures, aes(month, temperature, color = location)) + geom_boxplot(fill = "steelblue") ``` ] .center[ <img src="aesthetic-mappings_files/figure-html/ggplot-color-fill-params2-out-1.svg" width="80%" /> ] [//]: # "segment ends here" --- ## Further reading - Fundamentals of Data Visualization: [Chapter 2: Visualizing data](https://clauswilke.com/dataviz/aesthetic-mapping.html) - Data Visualization—A Practical Introduction: [Chapter 3: Make a plot](https://socviz.co/makeplot.html#makeplot) - [**ggplot2** reference documentation](https://ggplot2.tidyverse.org/reference/index.html) - [**ggplot2** book](https://ggplot2-book.org/) - [**ggplot2** cheatsheet](https://raw.githubusercontent.com/rstudio/cheatsheets/main/data-visualization.pdf)