class: center, middle, title-slide .title[ # Aesthetic Mapping -
ggplot2
foundations ] .author[ ### Claus O. Wilke, remixed by Joseph Elsherbini ] .date[ ### 09-01-2022 ] --- 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[ ``` Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0. ℹ Please use `linewidth` instead. ``` <!-- --> ] ??? Figure redrawn from [Claus O. Wilke. Fundamentals of Data Visualization. O'Reilly, 2019.](https://clauswilke.com/dataviz) --- ## Temperatures mapped onto color <br> .center[ <!-- --> ] ??? 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[ <!-- --> ] ??? 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 --- ## 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[ <!-- --> ] --- ## 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[ <!-- --> ] --- ## 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) ``` ] I prefer the long form, but if you see the abbreviated form now you know how it works. --- ## The geom determines how the data is shown .small-font[ ```r ggplot(temperatures, aes(day_of_year, temperature, color = location)) + geom_line() ``` ] .center[ <!-- --> ] --- ## 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[ <!-- --> ] --- ## The geom determines how the data is shown .small-font[ ```r ggplot(temperatures, aes(month, temperature, color = location)) + geom_boxplot() ``` ] .center[ <!-- --> ] --- ## 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[ <!-- --> ] [//]: # "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[ <!-- --> ] --- ## Many geoms have both `color` and `fill` aesthetics .tiny-font[ ```r ggplot(temperatures, aes(month, temperature, fill = location)) + geom_boxplot() ``` ] .center[ <!-- --> ] --- ## Many geoms have both `color` and `fill` aesthetics .tiny-font[ ```r ggplot(temperatures, aes(month, temperature, color = location, fill = location)) + geom_boxplot() ``` ] .center[ <!-- --> ] --- ## Aesthetics can also be used as parameters in geoms .tiny-font[ ```r ggplot(temperatures, aes(month, temperature, fill = location)) + geom_boxplot(color = "steelblue") ``` ] .center[ <!-- --> ] --- ## Aesthetics can also be used as parameters in geoms .tiny-font[ ```r ggplot(temperatures, aes(month, temperature, color = location)) + geom_boxplot(fill = "steelblue") ``` ] .center[ <!-- --> ] [//]: # "segment ends here" --- ## Exercise Time to try it out yourself! 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 Aesthetic Mapping excerise.