class: center, middle, inverse, title-slide <style> pre { background-color: lightyellow; white-space: pre-wrap; line-height: 100%; } </style> ## Data Visualization with R #### Jeremy Mack #### Lehigh University - Digital Scholarship Team <img src="./images/notes2.png" alt="RStudio" height=242/> --- class: center, middle, inverse, title-slide ## Data Visualization with R #### Jeremy Mack #### Lehigh University - Digital Scholarship Team <img src="./images/gganimate_hex.png" alt="RStudio" height=150/> <img src="./images/ggplot_hex.png" alt="RStudio" height=150/> <img src="./images/patchwork_hex.png" alt="RStudio" height=150/> <br/><br/> <br/><br/> <br/> --- ### About this presentation * This seminar is the second in a series on R programming, with a focus on **data visualization**. * It is targeted at people that have experience programming in R. * It could be useful for people who learned R some time ago and forgot it, or who are not familiar with modern R programming (`tidyverse`). * Slides are available on [Lehigh's Research Computing site](https://confluence.cc.lehigh.edu/display/hpc/Seminars) and Github ([slides](https://jeremymack-lu.github.io/rviz/) and [raw code](https://github.com/jeremymack-LU/rviz)) --- ### Structure of the presentation The presentation is split into four topics: * [**Topic 1:**](https://jeremymack-lu.github.io/rviz/#8) The (layered) Grammar of Graphics * [**Topic 2:**](https://jeremymack-lu.github.io/rviz/#22) Static visualizations * [**Topic 3:**](https://jeremymack-lu.github.io/rviz/#106) Dynamic visualizations * [**Topic 4:**](https://jeremymack-lu.github.io/rviz/#135) Shiny Apps - a preview --- #### Data Visualization with R <center><img src="./images/tidy1.png" alt="RStudio" height=350/></center> --- #### Data Visualization with R <center><img src="./images/tidy3.png" alt="RStudio" height=350/></center> --- #### Data Visualization with R <center><b>“The simple graph has brought more information to the data analyst’s mind than any other device.” - John Tukey</b></center> <br/> <center><img src="./images/dinosaur2.gif" alt="gif" height=200</></center> --- class: center, middle, inverse #### Topic 1: The (layered) Grammar of Graphics <br/><br/> <br/><br/> <br/><br/> <br/><br/> <br/><br/> --- #### Topic 1: The (layered) Grammar of Graphics .pull-left2[ * *The Grammar of Gaphics* * Structure of data graphics. * Foundation for modern applications. + **ggplot2** + Tableau + Vega-Lite] .pull-right2[ <center><img src="./images/grammar.png" alt="RStudio" height=350/></center>] --- #### Topic 1: The (layered) Grammar of Graphics .pull-left2[ * Tidyverse - **ggplot2** * Implementation of the grammar ] .pull-right2[ <center><img src="./images/ggplot2a.png" alt="RStudio" height=500/></center>] --- #### Topic 1: The (layered) Grammar of Graphics .pull-left2[ * Tidyverse - **ggplot2** * Implementation of the grammar * Layered approach to building graphics ] .pull-right2[ <center><img src="./images/ggplot1.png" alt="RStudio" width=500/></center>] --- #### Topic 1: The (layered) Grammar of Graphics .pull-left2[ * Tidyverse - **ggplot2** * Implementation of the grammar * Layered approach to building graphics + Data creates the base (tidy format) ] .pull-right2[ <center><img src="./images/ggplot2.png" alt="RStudio" width=500/></center>] --- #### Topic 1: The (layered) Grammar of Graphics .pull-left2[ * Tidyverse - **ggplot2** * Implementation of the grammar * Layered approach to building graphics + <span style="color:lightgray">Data creates the base (tidy format)</span> + Link variables to graphical properties ] .pull-right2[ <center><img src="./images/ggplot3.png" alt="RStudio" width=500/></center>] --- #### Topic 1: The (layered) Grammar of Graphics .pull-left2[ * Tidyverse - **ggplot2** * Implementation of the grammar * Layered approach to building graphics + <span style="color:lightgray">Data creates the base (tidy format)</span> + <span style="color:lightgray">Link variables to graphical properties</span> + Transform data to displayed values ] .pull-right2[ <center><img src="./images/ggplot4.png" alt="RStudio" width=500/></center>] --- #### Topic 1: The (layered) Grammar of Graphics .pull-left2[ * Tidyverse - **ggplot2** * Implementation of the grammar * Layered approach to building graphics + <span style="color:lightgray">Data creates the base (tidy format)</span> + <span style="color:lightgray">Link variables to graphical properties</span> + <span style="color:lightgray">Transform data to displayed values</span> + Variable and property ranges ] .pull-right2[ <center><img src="./images/ggplot5.png" alt="RStudio" width=500/></center>] --- #### Topic 1: The (layered) Grammar of Graphics .pull-left2[ * Tidyverse - **ggplot2** * Implementation of the grammar * Layered approach to building graphics + <span style="color:lightgray">Data creates the base (tidy format)</span> + <span style="color:lightgray">Link variables to graphical properties</span> + <span style="color:lightgray">Transform data to displayed values</span> + <span style="color:lightgray">Variable and property ranges</span> + Graphical representation of the data ] .pull-right2[ <center><img src="./images/ggplot6.png" alt="RStudio" width=500/></center>] --- #### Topic 1: The (layered) Grammar of Graphics .pull-left2[ * Tidyverse - **ggplot2** * Implementation of the grammar * Layered approach to building graphics + <span style="color:lightgray">Data creates the base (tidy format)</span> + <span style="color:lightgray">Link variables to graphical properties</span> + <span style="color:lightgray">Transform data to displayed values</span> + <span style="color:lightgray">Variable and property ranges</span> + <span style="color:lightgray">Graphical representation of the data</span> + Creates "small multiples" ] .pull-right2[ <center><img src="./images/ggplot7.png" alt="RStudio" width=500/></center>] --- #### Topic 1: The (layered) Grammar of Graphics .pull-left2[ * Tidyverse - **ggplot2** * Implementation of the grammar * Layered approach to building graphics + <span style="color:lightgray">Data creates the base (tidy format)</span> + <span style="color:lightgray">Link variables to graphical properties</span> + <span style="color:lightgray">Transform data to displayed values</span> + <span style="color:lightgray">Variable and property ranges</span> + <span style="color:lightgray">Graphical representation of the data</span> + <span style="color:lightgray">Creates "small multiples"</span> + Sets unique position ] .pull-right2[ <center><img src="./images/ggplot8.png" alt="RStudio" width=500/></center>] --- #### Topic 1: The (layered) Grammar of Graphics .pull-left2[ * Tidyverse - **ggplot2** * Implementation of the grammar * Layered approach to building graphics + <span style="color:lightgray">Data creates the base (tidy format)</span> + <span style="color:lightgray">Link variables to graphical properties</span> + <span style="color:lightgray">Transform data to displayed values</span> + <span style="color:lightgray">Variable and property ranges</span> + <span style="color:lightgray">Graphical representation of the data</span> + <span style="color:lightgray">Creates "small multiples"</span> + <span style="color:lightgray">Sets unique position</span> + Defines the overall look of the plot ] .pull-right2[ <center><img src="./images/ggplot9.png" alt="RStudio" width=500/></center>] --- #### Topic 1: The (layered) Grammar of Graphics .pull-left2[ * Tidyverse - **ggplot2** * Implementation of the grammar * Layered approach to building graphics + Required arguments + Everything else has **sensible defaults** ] .pull-right2[ <center><img src="./images/ggplot10.png" alt="RStudio" width=500/></center>] --- #### Topic 1: The (layered) Grammar of Graphics .pull-left2[ * Tidyverse - **ggplot2** * Implementation of the grammar * Layered approach to building graphics + Required arguments + Everything else has **sensible defaults** * Basic template has three functions: + **ggplot** - creates a blank canvas + **geom** - adds a layer + **aes** - visual properties of an object ] .pull-right2[ <center><img src="./images/ggplot10.png" alt="RStudio" width=500/></center> <center><img src="./images/ggplot.png" alt="RStudio" width=500/></center> ] --- class: center, middle, inverse #### Topic 2: Static visualizations #### "Old Faithful" <br/><br/> <br/><br/> <br/><br/> <br/><br/> <br/><br/> --- #### Topic 2: Static visualizations .pull-right2[ <center><img src="./images/OldFaithfulGeyser_Yellowstone.jpg" alt="RStudio" width=500/></center> ] .tiny2.pull-left2[ * Old Faithful dataset: + Geyser in Yellowstone National Park + Eruption time in minutes (*eruptions*) + Time until next eruption (*waiting*) {{content}} ] -- ```r head(faithful, 5) ``` ``` eruptions waiting 1 3.600 79 2 1.800 54 3 3.333 74 4 2.283 62 5 4.533 85 ``` {{content}} ```r str(faithful) ``` ``` 'data.frame': 272 obs. of 2 variables: $ eruptions: num 3.6 1.8 3.33 2.28 4.53 ... $ waiting : num 79 54 74 62 85 55 88 85 51 85 ... ``` {{content}} --- #### Topic 2: Static visualizations .right-column2[ <center><img src="./images/ggplot10.png" alt="RStudio" width=500/></center> <center><img src="./images/ggplot.png" alt="RStudio" width=500/></center> ] .left-column2[ * Layered approach to building graphics .tiny[ ```r # Create a blank ggplot canvas ggplot(data=faithful) ``` ] ] --- #### Topic 2: Static visualizations <center><img src="./images/p1.png" alt="ggplot"</></center> --- #### Topic 2: Static visualizations .right-column2[ <center><img src="./images/ggplot10.png" alt="RStudio" width=500/></center> <center><img src="./images/ggplot.png" alt="RStudio" width=500/></center> ] .left-column2[ * Layered approach to building graphics .tiny[ ```r # Create a blank ggplot canvas ggplot(data=faithful) ``` ```r # Add a geometric (geom) layer and map variables to aesthetics (aes) function ggplot(data=faithful) + geom_point(mapping=aes(x=eruptions, y=waiting)) ``` ] ] --- #### Topic 2: Static visualizations <center><img src="./images/p3.png" alt="ggplot"</></center> --- #### Topic 2: Static visualizations .right-column2[ <center><img src="./images/ggplot10.png" alt="RStudio" width=500/></center> <center><img src="./images/ggplot.png" alt="RStudio" width=500/></center> ] .left-column2[ * Layered approach to building graphics * Some things to remember: + Multiple ways to map variables ] --- #### Topic 2: Static visualizations .right-column2[ <center><img src="./images/ggplot10.png" alt="RStudio" width=500/></center> <center><img src="./images/ggplot.png" alt="RStudio" width=500/></center> ] .tiny.left-column2[ * Split mappings: ```r ggplot(data=faithful) + geom_point(mapping=aes(x=eruptions, y=waiting)) ``` {{content}} ] -- * Mappings in **ggplot** function: ```r ggplot(data=faithful, mapping=aes(x=eruptions, y=waiting)) + geom_point() ``` {{content}} -- * Mappings in **geom** function: ```r ggplot() + geom_point(data=faithful, mapping=aes(x=eruptions, y=waiting)) ``` {{content}} --- #### Topic 2: Static visualizations .right-column2[ <center><img src="./images/ggplot10.png" alt="RStudio" width=500/></center> <center><img src="./images/ggplot.png" alt="RStudio" width=500/></center> ] .left-column2[ * Layered approach to building graphics .tiny[ ```r # Create a blank ggplot canvas ggplot(data=faithful) ``` ```r # Add a geometric (geom) layer and map variables to aesthetics (aes) function ggplot(data=faithful) + geom_point(mapping=aes(x=eruptions, y=waiting)) ``` ```r # Add another aesthetic ggplot(data=faithful) + geom_point(mapping=aes(x=eruptions, y=waiting, color=eruptions < 3)) ``` ] ] --- #### Topic 2: Static visualizations <center><img src="./images/p4.png" alt="ggplot"</></center> --- #### Topic 2: Static visualizations .right-column2[ <center><img src="./images/ggplot10.png" alt="RStudio" width=500/></center> <center><img src="./images/ggplot.png" alt="RStudio" width=500/></center> ] .left-column2[ * Layered approach to building graphics * Some things to remember: + <span style="color:lightgray">Multiple ways to map variables</span> + Multiple ways to change aesthetics ] --- #### Topic 2: Static visualizations .right-column2[ <center><img src="./images/ggplot10.png" alt="RStudio" width=500/></center> <center><img src="./images/ggplot.png" alt="RStudio" width=500/></center> ] .tiny.left-column2[ * Mapping a color: ```r ggplot(data=faithful) + geom_point(mapping=aes(x=eruptions, y=waiting, color=eruptions < 3)) ``` {{content}} ] -- * Setting a color: ```r ggplot(data=faithful) + geom_point(mapping=aes(x=eruptions, y=waiting), color='blue') ``` {{content}} --- #### Topic 2: Static visualizations <center><img src="./images/p5.png" alt="ggplot"</></center> --- #### Topic 2: Static visualizations .right-column2[ <center><img src="./images/ggplot10.png" alt="RStudio" width=500/></center> <center><img src="./images/ggplot.png" alt="RStudio" width=500/></center> ] .tiny.left-column2[ * Mapping a color: ```r ggplot(data=faithful) + geom_point(mapping=aes(x=eruptions, y=waiting, color=eruptions < 3)) ``` * Setting a color: ```r ggplot(data=faithful) + geom_point(mapping=aes(x=eruptions, y=waiting), color='blue') ``` * Setting inside aes function? ```r ggplot(data=faithful) + geom_point(mapping=aes(x=eruptions, y=waiting, color='blue')) ``` ] --- #### Topic 2: Static visualizations <center><img src="./images/p5a.png" alt="ggplot"</></center> --- #### Topic 2: Static visualizations .right-column2[ <center><img src="./images/ggplot10.png" alt="RStudio" width=500/></center> <center><img src="./images/ggplot.png" alt="RStudio" width=500/></center> ] .left-column2[ * Layered approach to building graphics * Some things to remember: + <span style="color:lightgray">Multiple ways to map variables</span> + <span style="color:lightgray">Multiple ways to change aesthetics</span> + Aesthetics vary by geometry ] --- #### Topic 2: Static visualizations <center><img src="./images/geoms.png" height=550 alt="ggplot"</></center> --- #### Topic 2: Static visualizations .right-column2[ <center><img src="./images/ggplot10.png" alt="RStudio" width=500/></center> <center><img src="./images/ggplot.png" alt="RStudio" width=500/></center> ] .tiny.left-column2[ * Mapping a color: ```r ggplot(data=faithful) + geom_point(mapping=aes(x=eruptions, y=waiting, color=eruptions < 3)) ``` {{content}} ] -- * Adding aesthetics: ```r ggplot(data=faithful) + geom_point(mapping=aes(x=eruptions, y=waiting, color=eruptions < 3, size=waiting), alpha=0.5) ``` {{content}} --- #### Topic 2: Static visualizations <center><img src="./images/p5e.png" alt="ggplot"</></center> --- #### Topic 2: Static visualizations .right-column2[ <center><img src="./images/ggplot10.png" alt="RStudio" width=500/></center> <center><img src="./images/ggplot.png" alt="RStudio" width=500/></center> ] .left-column2[ * Layered approach to building graphics * Some things to remember: + <span style="color:lightgray">Multiple ways to map variables</span> + <span style="color:lightgray">Multiple ways to change aesthetics</span> + <span style="color:lightgray">Aesthetics vary by geometry</span> + When stacking layers, order matters ] --- #### Topic 2: Static visualizations .right-column2[ <center><img src="./images/ggplot10.png" alt="RStudio" width=500/></center> <center><img src="./images/ggplot.png" alt="RStudio" width=500/></center> ] .tiny.left-column2[ * Add another geom ("stacking layers"): ```r ggplot(data=faithful, mapping=aes(x=eruptions, y=waiting)) + geom_point() + geom_smooth(method="lm") ``` ] --- #### Topic 2: Static visualizations <center><img src="./images/p7.png" alt="ggplot"</></center> --- #### Topic 2: Static visualizations .right-column2[ <center><img src="./images/ggplot10.png" alt="RStudio" width=500/></center> <center><img src="./images/ggplot.png" alt="RStudio" width=500/></center> ] .tiny.left-column2[ * Add another geom ("stacking layers"): ```r ggplot(data=faithful, mapping=aes(x=eruptions, y=waiting)) + geom_point() + geom_smooth(method="lm") ``` {{content}} ] -- * Order matters ```r ggplot(data=faithful, mapping=aes(x=eruptions, y=waiting)) + geom_smooth(method="lm") + geom_point() ``` {{content}} --- #### Topic 2: Static visualizations <center><img src="./images/p7b.png" alt="ggplot"</></center> --- #### Topic 2: Static visualizations .right-column2[ <center><img src="./images/ggplot10.png" alt="RStudio" width=500/></center> <center><img src="./images/ggplot.png" alt="RStudio" width=500/></center> ] .left-column2[ * Layered approach to building graphics * Some things to remember: + <span style="color:lightgray">Multiple ways to map variables</span> + <span style="color:lightgray">Multiple ways to change aesthetics</span> + <span style="color:lightgray">Aesthetics vary by geometry</span> + <span style="color:lightgray">When stacking layers, order matters</span> + Many types of geoms, different mapping requirements ] --- #### Topic 2: Static visualizations <center><img src="./images/geoms.png" height=550 alt="ggplot"</></center> --- #### Topic 2: Static visualizations .right-column2[ <center><img src="./images/ggplot9.png" alt="RStudio" width=500/></center> <center><img src="./images/ggplot.png" alt="RStudio" width=500/></center> ] .left-column2[ * Layered approach to building graphics * Some things to remember: + <span style="color:lightgray">Multiple ways to map variables</span> + <span style="color:lightgray">Multiple ways to change aesthetics</span> + <span style="color:lightgray">Aesthetics vary by geometry</span> + <span style="color:lightgray">When stacking layers, order matters</span> + <span style="color:lightgray">Many types of geoms, different mapping requirements</span> + Many (many!) theme arguments ] --- #### Topic 2: Static visualizations .right-column2[ <center><img src="./images/ggplot9.png" alt="RStudio" width=500/></center> <center><img src="./images/ggplot.png" alt="RStudio" width=500/></center> ] .tiny2.left-column2[ * Layered approach to building graphics ```r # Create overall plot object plot <- ggplot(data=faithful, mapping=aes(x=eruptions, y=waiting)) + geom_smooth(method="lm") + geom_point(size=0.25) ``` {{content}} ] -- ```r # Clean up overall plot theme plot <- plot + theme(plot.background=element_blank(), panel.border=element_blank(), panel.background=element_blank(), axis.ticks=element_line(size=0.25), axis.text=element_text(size = 7), axis.line=element_line(size=0.25), axis.title=element_text(size = 7), legend.text=element_text(size = 7), legend.title=element_text(size = 7), legend.key=element_blank(), plot.title=element_text(hjust=.5, size=8)) + labs(title="Old Faithful Waiting Time", x="Length of eruption (minutes)", y="Waiting time to next eruption (minutes)") ``` {{content}} --- #### Topic 2: Static visualizations <center><img src="./images/faithful.png" alt="ggplot"</></center> --- class: center, middle, inverse #### Topic 2: Static visualizations #### ggplot2 extensions <br/><br/> <br/><br/> <br/><br/> <br/><br/> <br/><br/> --- #### Topic 2: Static visualizations .right-column2[ <center><img src="./images/ggplot_hex.png" height=250 alt="RStudio" </center> ] .left-column2[ * **ggplot2** extensions * Extends the grammar of graphics + Developed by R users + Provides additional [functionality](<http://exts.ggplot2.tidyverse.org/gallery/>) ] --- #### Topic 2: Static visualizations .right-column2[ <center><img src="./images/patchwork_hex.png" height=250 alt="RStudio" </center> ] .left-column2[ * **ggplot2** extensions - **patchwork** * Extends the grammar of graphics + Arrange multiple plots, using mathematical operators + Many approaches - Facets - gridExtra::grid.arrange() - ggpubr::ggarrange() - cowplot::plot_grid() - patchwork ] --- #### Topic 2: Static visualizations .tiny.left-column3[ * Establish plots: ```r library(patchwork) pw1 <- ggplot(data=faithful, mapping=aes(x=eruptions, y=waiting)) + geom_point() pw2 <- ggplot(data=faithful, mapping=aes(x=eruptions, y=waiting)) + geom_point() + geom_smooth(method="lm") ``` ] -- .tiny.right-column3[ * Patchwork together: ```r # Side by side pw1 + pw2 ``` {{content}} ] --- #### Topic 2: Static visualizations <center><img src="./images/pw1.png" alt="ggplot"</></center> --- #### Topic 2: Static visualizations .tiny.left-column3[ * Establish plots: ```r library(patchwork) pw1 <- ggplot(data=faithful, mapping=aes(x=eruptions, y=waiting)) + geom_point() pw2 <- ggplot(data=faithful, mapping=aes(x=eruptions, y=waiting)) + geom_point() + geom_smooth(method="lm") ``` ] .tiny.right-column3[ * Patchwork together: ```r # Side by side pw1 + pw2 # Stacked pw1 / pw2 ``` {{content}} ] --- #### Topic 2: Static visualizations <center><img src="./images/pw2.png" alt="ggplot"</></center> --- #### Topic 2: Static visualizations .tiny.left-column3[ * Establish plots: ```r library(patchwork) pw1 <- ggplot(data=faithful, mapping=aes(x=eruptions, y=waiting)) + geom_point() pw2 <- ggplot(data=faithful, mapping=aes(x=eruptions, y=waiting)) + geom_point() + geom_smooth(method="lm") pw3 <- ggplot(data=faithful, mapping=aes(x=eruptions)) + geom_histogram() ``` ] .tiny.right-column3[ * Patchwork together: ```r # Side by side pw1 + pw2 # Stacked pw1 / pw2 # Three figures (pw1 + pw2) / pw3 ``` {{content}} ] --- #### Topic 2: Static visualizations <center><img src="./images/pw3.png" alt="ggplot"</></center> --- #### Topic 2: Static visualizations <center><img src="./images/pw1.png" alt="ggplot"</></center> --- #### Topic 2: Static visualizations .tiny.left-column3[ * Establish plots: ```r library(patchwork) pw1 <- ggplot(data=faithful, mapping=aes(x=eruptions, y=waiting)) + geom_point() pw2 <- ggplot(data=faithful, mapping=aes(x=eruptions, y=waiting)) + geom_point() + geom_smooth(method="lm") ``` ] .tiny.right-column3[ * Patchwork together: ```r # Side by side pw1 + pw2 ``` ] --- #### Topic 2: Static visualizations .tiny.left-column3[ * Establish plots: ```r library(patchwork) pw1 <- ggplot(data=faithful, mapping=aes(x=eruptions, y=waiting)) + geom_point() pw2 <- ggplot(data=faithful, mapping=aes(x=eruptions, y=waiting)) + geom_point() + geom_smooth(method="lm") ``` ] .tiny.right-column3[ * Patchwork together: ```r # Side by side pw1 + pw2 + plot_layout(widths=c(2,1)) ``` ] --- #### Topic 2: Static visualizations <center><img src="./images/pw5.png" alt="ggplot"</></center> --- #### Topic 2: Static visualizations .tiny.left-column3[ * Establish plots: ```r library(patchwork) pw1 <- ggplot(data=faithful, mapping=aes(x=eruptions, y=waiting)) + geom_point() pw2 <- ggplot(data=faithful, mapping=aes(x=eruptions, y=waiting)) + geom_point() + geom_smooth(method="lm") ``` ] .tiny.right-column3[ * Patchwork together: ```r # Side by side pw1 + pw2 ``` {{content}} ] -- ```r library(grid); library(gridExtra) # Remove axis title pw1 <- pw1 + theme(axis.title=element_blank()) pw2 <- pw2 + theme(axis.title=element_blank()) # Patch together and create a 'grob' object patch <- pw1 + pw2 patch.gr <- patchworkGrob(patch) # Add grid text grid.arrange(patch.gr, left=textGrob("Time until next eruption", gp=gpar(fontsize=8), rot=90), bottom=textGrob("Length of eruption", gp=gpar(fontsize=8))) ``` {{content}} --- #### Topic 2: Static visualizations <center><img src="./images/pw4.png" alt="ggplot"</></center> --- #### Topic 2: Static visualizations .right-column2[ <center><img src="./images/ggforce_hex.png" height=250 alt="RStudio" </center> ] .left-column2[ * **ggplot2** extensions - **ggforce/ggthemes** * Extends the grammar of graphics + Provides extra geoms, scales, and themes ] --- #### Topic 2: Static visualizations .right-column2[ <center><img src="./images/ggforce_hex.png" height=250 alt="RStudio" </center> ] .tiny.left-column2[ * **ggplot2** extensions - **ggforce/ggthemes** * Extends the grammar of graphics + Provides extra geoms, scales, and themes + Microsoft Excel theme ```r library(ggthemes) ggplot(data=faithful) + geom_point(mapping=aes( x=eruptions, y=waiting), color='blue') + theme_excel() ``` ] --- #### Topic 2: Static visualizations <center><img src="./images/p5b.png" alt="ggplot"</></center> --- #### Topic 2: Static visualizations .right-column2[ <center><img src="./images/ggforce_hex.png" height=250 alt="RStudio" </center> ] .tiny.left-column2[ * **ggplot2** extensions - **ggforce/ggthemes** * Extends the grammar of graphics + Provides extra geoms, scales, and themes + Microsoft Excel theme + Wall Street Journal theme ```r library(ggthemes) ggplot(data=faithful) + geom_point(mapping=aes( x=eruptions, y=waiting), color='blue') + #theme_excel() theme_wsj() ``` ] --- #### Topic 2: Static visualizations <center><img src="./images/p5c.png" alt="ggplot"</></center> --- #### Topic 2: Static visualizations .right-column2[ <center><img src="./images/ggforce_hex.png" height=250 alt="RStudio" </center> ] .tiny.left-column2[ * **ggplot2** extensions - **ggforce/ggthemes** * Extends the grammar of graphics + Provides extra geoms, scales, and themes + Microsoft Excel theme + Wall Street Journal theme + Google Docs theme ```r library(ggthemes) ggplot(data=faithful) + geom_point(mapping=aes( x=eruptions, y=waiting), color='blue') + #theme_excel() #theme_wsj() theme_gdocs() ``` ] --- #### Topic 2: Static visualizations <center><img src="./images/p5d.png" alt="ggplot"</></center> --- #### Topic 2: Static visualizations .right-column2[ <center><img src="./images/ggstats_hex.png" height=250 alt="RStudio" </center> ] .tiny.left-column2[ * **ggplot2** extensions - **ggstatsplot** * Extends the grammar of graphics + Quickly combine plots with statistics {{content}} ] -- ```r ggscatterstats( data=faithful, x=eruptions, y=waiting, xlab="Length of eruption", ylab="Time until next eruption", title="How long until Old Faithful erupts?") ``` --- #### Topic 2: Static visualizations <center><img src="./images/stats.png" alt="ggplot"</></center> --- #### Topic 2: Static visualizations [<center><img src="./images/ggstatsplot.png" alt="ggplot"</></center>](https://indrajeetpatil.github.io/ggstatsplot/) --- class: center, middle, inverse #### Topic 2: Static visualizations #### ggplot2 and simple features (sf) data <br/><br/> <br/><br/> <br/><br/> <br/><br/> <br/><br/> --- #### Topic 2: Static visualizations .right-column2[ <center><img src="./images/sf_hex.png" height=250 alt="RStudio" </center> ] .left-column2[ * Simple features (**sf**) data * Introduces a new type of object to work with spatial data {{content}} ] --- #### Topic 2: Static visualizations <style> pre { background-color: lightyellow; white-space: pre-wrap; line-height: 90%; } </style> .right-column2[ <center><img src="./images/sf.jpg" height=250 alt="RStudio" </center> ] .left-column2[ * Simple features (**sf**) data * Introduces a new type of object to work with spatial data + Non-spatial data + Spatial data + Attributes + Geometries ] --- class: center, middle, inverse #### Comparison of sf and non-sf spatial data <br/><br/> <br/><br/> <br/><br/> <br/><br/> <br/><br/> --- #### Topic 2: Static visualizations .left-column5b[ * Spatial data in a data frame .tiny7[ ```r library(usmap) # State and county data non-sf non_sf <- us_map(regions="states"); head(non_sf, 5); str(non_sf) ``` ``` ## x y order hole piece group fips abbr full ## 1 1091779 -1380695 1 FALSE 1 01.1 01 AL Alabama ## 2 1091268 -1376372 2 FALSE 1 01.1 01 AL Alabama ## 3 1091140 -1362998 3 FALSE 1 01.1 01 AL Alabama ## 4 1090940 -1343517 4 FALSE 1 01.1 01 AL Alabama ## 5 1090913 -1341006 5 FALSE 1 01.1 01 AL Alabama ``` ``` ## 'data.frame': 12999 obs. of 9 variables: ## $ x : num 1091779 1091268 1091140 1090940 1090913 ... ## $ y : num -1380695 -1376372 -1362998 -1343517 -1341006 ... ## $ order: int 1 2 3 4 5 6 7 8 9 10 ... ## $ hole : logi FALSE FALSE FALSE FALSE FALSE FALSE ... ## $ piece: int 1 1 1 1 1 1 1 1 1 1 ... ## $ group: chr "01.1" "01.1" "01.1" "01.1" ... ## $ fips : chr "01" "01" "01" "01" ... ## $ abbr : chr "AL" "AL" "AL" "AL" ... ## $ full : chr "Alabama" "Alabama" "Alabama" "Alabama" ... ``` ] ] -- .right-column5b[ * Plot of Pennsylvania .tiny7[ ```r non_sf %>% filter(abbr=="PA") %>% ggplot() + geom_point(aes(x=x,y=y,group=group)) + geom_path(aes(x=x,y=y,group=group)) + coord_fixed() + ggthemes::theme_map() ``` ] <center><img src="./images/pa_state.png" alt="ggplot"</></center> ] --- #### Topic 2: Static visualizations .left-column5b[ * Spatial data in an sf object .tiny7[ ```r library(urbnmapr) # State and county shapefiles in sf states_sf <- get_urbn_map("states", sf = TRUE); print(states_sf) ``` ``` ## Simple feature collection with 51 features and 3 fields ## Geometry type: MULTIPOLYGON ## Dimension: XY ## Bounding box: xmin: -2600000 ymin: -2363000 xmax: 2516374 ymax: 732352.2 ## Projected CRS: NAD27 / US National Atlas Equal Area ## First 10 features: ## state_fips state_abbv state_name geometry ## 1 01 AL Alabama MULTIPOLYGON (((1150023 -15... ## 2 04 AZ Arizona MULTIPOLYGON (((-1386136 -1... ## 3 08 CO Colorado MULTIPOLYGON (((-786661.9 -... ## 4 09 CT Connecticut MULTIPOLYGON (((2156197 -83... ## 5 12 FL Florida MULTIPOLYGON (((1953691 -20... ## 6 13 GA Georgia MULTIPOLYGON (((1308636 -10... ## 7 16 ID Idaho MULTIPOLYGON (((-1357097 78... ## 8 18 IN Indiana MULTIPOLYGON (((1042064 -71... ## 9 20 KS Kansas MULTIPOLYGON (((-174904.2 -... ## 10 22 LA Louisiana MULTIPOLYGON (((1075669 -15... ``` ] ] -- .right-column5b[ * Plot of Pennsylvania .tiny7[ ```r states_sf %>% filter(state_abbv=='PA') %>% ggplot() + geom_sf() + ggthemes::theme_map() ``` ] <center><img src="./images/pa_state2.png" alt="ggplot"</></center> ] --- #### Topic 2: Static visualizations .left-column5[ * Spatial data in a data frame .tiny7[ ```r library(usmap) # State and county data non-sf non_sf <- us_map(regions="states"); head(non_sf, 5); str(non_sf) ``` ``` ## x y order hole piece group fips abbr full ## 1 1091779 -1380695 1 FALSE 1 01.1 01 AL Alabama ## 2 1091268 -1376372 2 FALSE 1 01.1 01 AL Alabama ## 3 1091140 -1362998 3 FALSE 1 01.1 01 AL Alabama ## 4 1090940 -1343517 4 FALSE 1 01.1 01 AL Alabama ## 5 1090913 -1341006 5 FALSE 1 01.1 01 AL Alabama ``` ``` ## 'data.frame': 12999 obs. of 9 variables: ## $ x : num 1091779 1091268 1091140 1090940 1090913 ... ## $ y : num -1380695 -1376372 -1362998 -1343517 -1341006 ... ## $ order: int 1 2 3 4 5 6 7 8 9 10 ... ## $ hole : logi FALSE FALSE FALSE FALSE FALSE FALSE ... ## $ piece: int 1 1 1 1 1 1 1 1 1 1 ... ## $ group: chr "01.1" "01.1" "01.1" "01.1" ... ## $ fips : chr "01" "01" "01" "01" ... ## $ abbr : chr "AL" "AL" "AL" "AL" ... ## $ full : chr "Alabama" "Alabama" "Alabama" "Alabama" ... ``` ] ] .right-column5[ * Spatial data in an sf object .tiny7[ ```r library(urbnmapr) # State and county shapefiles in sf states_sf <- get_urbn_map("states", sf = TRUE); print(states_sf) ``` ``` ## Simple feature collection with 51 features and 3 fields ## Geometry type: MULTIPOLYGON ## Dimension: XY ## Bounding box: xmin: -2600000 ymin: -2363000 xmax: 2516374 ymax: 732352.2 ## Projected CRS: NAD27 / US National Atlas Equal Area ## First 10 features: ## state_fips state_abbv state_name geometry ## 1 01 AL Alabama MULTIPOLYGON (((1150023 -15... ## 2 04 AZ Arizona MULTIPOLYGON (((-1386136 -1... ## 3 08 CO Colorado MULTIPOLYGON (((-786661.9 -... ## 4 09 CT Connecticut MULTIPOLYGON (((2156197 -83... ## 5 12 FL Florida MULTIPOLYGON (((1953691 -20... ## 6 13 GA Georgia MULTIPOLYGON (((1308636 -10... ## 7 16 ID Idaho MULTIPOLYGON (((-1357097 78... ## 8 18 IN Indiana MULTIPOLYGON (((1042064 -71... ## 9 20 KS Kansas MULTIPOLYGON (((-174904.2 -... ## 10 22 LA Louisiana MULTIPOLYGON (((1075669 -15... ``` ] ] --- #### Topic 2: Static visualizations <center><img src="./images/cdc_masking_map2.jpeg" alt="ggplot"</></center> --- #### Topic 2: Static visualizations .pull-right2[ <center><img src="./images/sf.jpg" height=250 alt="RStudio" </center> ] .tiny2.pull-left2[ Necessary datasets: * Spatial data: state and county boundaries * Non-spatial data: CDC COVID-19 metrics ] --- #### Topic 2: Static visualizations .right-column2[ <center><img src="./images/map2.png" width=300 alt="RStudio" </center> <br/> <center><img src="./images/map1.png" width=300 alt="RStudio" </center> <br/> <center><img src="./images/map3.png" width=300 alt="RStudio" </center> ] .left-column2[ * Spatial data: state and county boundaries .tiny3[ ```r # Install albersusa package if needed #remotes::install_github("hrbrmstr/albersusa") # Load required packages library(albersusa, sf, ggtext) # County boundary sf object cty.sf <- counties_sf("longlat") sf::st_crs(cty.sf) <- sf::st_crs(cty.sf) # State boundary sf object usa.sf <- usa_sf("longlat") sf::st_crs(usa.sf) <- sf::st_crs(usa.sf) # County boundary filtered for Pennsylvania pa.sf <- cty.sf %>% filter(name=="Pennsylvania") ``` ] ] --- #### Topic 2: Static visualizations .tiny3.left-column8[ * Non-spatial data: CDC COVID-19 metrics ```r # Read cdc covid-19 community levels from cdc website url <- "https://www.cdc.gov/coronavirus/2019-ncov/json/covid-community-level-structure-2.csv" cdc <- read_csv(url, skip=1) cdc <- cdc %>% #select(-c(10:12)) %>% rename(fips=2, hosp_beds=3, hosp_admin=4, cases_per_100k=5, comm_level=6, cases=7, positivity=8, comm_trans_level=9) ``` ``` ## # A tibble: 6 × 9 ## County fips hosp_beds hosp_admin cases_per_100k comm_level cases positivity ## <chr> <dbl> <chr> <dbl> <dbl> <chr> <lgl> <lgl> ## 1 Autauga… 1001 1.90% 2.6 35.8 Low NA NA ## 2 Bullock… 1011 1.90% 2.6 0 Low NA NA ## 3 Covingt… 1039 1.90% 2.6 32.4 Low NA NA ## 4 Crensha… 1041 1.90% 2.6 36.3 Low NA NA ## 5 Lowndes… 1085 1.90% 2.6 20.6 Low NA NA ## 6 Montgom… 1101 1.90% 2.6 25.6 Low NA NA ## # … with 1 more variable: comm_trans_level <lgl> ``` ] --- #### Topic 2: Static visualizations .tiny3.left-column8[ * Combine spatial and non-spatial data ```r # Join cdc covid-19 community levels to county boundary sf object cty.sf <- cty.sf %>% select(fips,state) %>% mutate(fips=as.numeric(as.character(fips))) %>% left_join(cdc,by='fips') %>% mutate(comm_level=case_when(is.na(comm_level) ~ "No Data", TRUE ~ comm_level)) %>% select(fips,state,comm_level) ``` ``` ## Simple feature collection with 6 features and 3 fields ## Geometry type: MULTIPOLYGON ## Dimension: XY ## Bounding box: xmin: -88.13999 ymin: 31.19518 xmax: -85.12342 ymax: 34.8922 ## Geodetic CRS: WGS 84 ## fips state comm_level geometry ## 1 1001 Alabama Low MULTIPOLYGON (((-86.49677 3... ## 2 1009 Alabama Low MULTIPOLYGON (((-86.5778 33... ## 3 1017 Alabama Low MULTIPOLYGON (((-85.18413 3... ## 4 1021 Alabama Medium MULTIPOLYGON (((-86.51734 3... ## 5 1033 Alabama Low MULTIPOLYGON (((-88.13999 3... ## 6 1045 Alabama Low MULTIPOLYGON (((-85.41644 3... ``` ] --- #### Topic 2: Static visualizations .tiny3.left-column8[ * Set colors and specify elements of the plot's theme ```r library(ggtext) ``` ```r # Specify color scheme cols <- c("Low"="#197d7d", "Medium"="#dbc037", "High"="#e08f38", "No Data"="lightgray") # Specify plot theme plot.theme <- theme( # Add extra space to the right margin for inset map plot.margin = unit(c(0, 2.5, 0, 0), "cm"), legend.position=c(0.5,0.98), legend.direction="horizontal", legend.title=element_blank(), legend.key.height=unit(0.25, 'cm'), legend.key.width=unit(0.25, 'cm'), legend.text=element_text(size=6), legend.margin=margin(b=5.5), text=element_text(color="#22211d"), plot.background=element_rect(fill="white", color=NA), panel.background=element_rect(fill="white", color=NA), plot.title=element_textbox_simple(size=8, color="#4e4d47", padding=margin(5.5, 5.5, 5.5, 5.5)), plot.subtitle=element_textbox_simple(size=6, color="#4e4d47", face="italic", padding=margin(0, 5.5, 5.5, 5.5))) ``` ] --- #### Topic 2: Static visualizations .tiny3.left-column8[ * Create the main plot ```r plot <- ggplot() + geom_sf(data=cty.sf, aes(fill=comm_level), size=0.1, color="white", alpha=0.9) + geom_sf(data=usa.sf, size=0.1, color="black", fill=NA, alpha=0.9) + scale_fill_manual(values=cols) + labs(title="COVID-19 CDC Community Levels", subtitle=paste( "CDC metric that factors in new COVID-19 hospitalizations, existing hospital capacity, and new cases in an area. Data as of", Sys.Date(),"\n")) + theme_void() + plot.theme ``` ] --- #### Topic 2: Static visualizations <center><img src="./images/cdc_masking_map1.jpeg" alt="ggplot"</></center> --- #### Topic 2: Static visualizations .tiny3.left-column8[ * Add inset map of PA with Cowplot package ```r library(cowplot) mask <- ggdraw(plot) + draw_plot( { cty.sf %>% filter(state=="Pennsylvania") %>% ggplot() + geom_sf(aes(fill=comm_level), size=0.1, color="white", alpha=0.9) + geom_sf(data=pa.sf,size=0.1,color="black",fill=NA,alpha=0.9) + scale_fill_manual(values=cols) + theme_void() + theme( legend.position="none", plot.background=element_rect(fill="white", color=NA), panel.background=element_rect(fill="white", color=NA))}, x = 0.68, y = 0.2, width = 0.3, height = 0.3) ``` ] --- #### Topic 2: Static visualizations <center><img src="./images/cdc_masking_map2.jpeg" alt="ggplot"</></center> --- class: inverse #### Review - Grammar of Graphics & Static Visualizations: * Grammar of graphics - basis for many data visualization programs. * **ggplot2** is a popular implementation. * Layered approach with sensible defaults. <center><img src="./images/ggplot.png" alt="RStudio" width=400/></center> * Extension packages expand **ggplot2** capabilities. * **sf** data makes working with spatial data more efficient. <br/><br/> .pull-right3[ <img src="./images/patchwork_hex.png" alt="RStudio" height=150/ align="right"> <img src="./images/ggstats_hex.png" alt="RStudio" height=150/ align="right" style="vertical-align:middle;margin:0px 10px"> <img src="./images/ggplot_hex.png" alt="RStudio" height=150/ align="right"> ] --- class: center, middle, inverse #### Topic 3: Dynamic visualizations #### "Life expectency" <br/><br/> <br/><br/> <br/><br/> <br/><br/> <br/><br/> --- #### Topic 3: Dynamic visualizations .pull-right2[ <center><img src="./images/gapminder2.png" alt="RStudio" width=250/></center> ] .tiny2.pull-left2[ * Life expectency (**gapminder**) dataset: + Data from the Gapminder foundation + Geography (*country* and *continent*) + Life expectency (*lifeExp*) + Economic data (*gdpPercap*) + Time (*year*) ] --- #### Topic 3: Dynamic visualizations .pull-right2[ <center><img src="./images/gapminder2.png" alt="RStudio" width=250/></center> ] .pull-left2[ * Life expectency (**gapminder**) dataset: + Data from the Gapminder foundation + Geography (*country* and *continent*) + Life expectency (*lifeExp*) + Economic data (*gdpPercap*) + Time (*year*) ] <br/><br/> <br/><br/> <br/><br/> <br/><br/> <br/><br/> <br/><br/> .tiny3[ ```r str(gapminder) ``` ``` tibble [1,704 × 6] (S3: tbl_df/tbl/data.frame) $ country : Factor w/ 142 levels "Afghanistan",..: 1 1 1 1 1 1 1 1 1 1 ... $ continent: Factor w/ 5 levels "Africa","Americas",..: 3 3 3 3 3 3 3 3 3 3 ... $ year : int [1:1704] 1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ... $ lifeExp : num [1:1704] 28.8 30.3 32 34 36.1 ... $ pop : int [1:1704] 8425333 9240934 10267083 11537966 13079460 14880372 12881816 13867957 16317921 22227415 ... $ gdpPercap: num [1:1704] 779 821 853 836 740 ... ``` ] --- #### Topic 3: Dynamic visualizations <center><img src="./images/gapminder.gif" width=700 alt="ggplot"</></center> --- #### Topic 3: Dynamic visualizations .right-column2[ <center><img src="./images/gganimate_hex.png" height=250 alt="RStudio" </center> ] .left-column2[ * **ggplot2** extensions - **gganimate** * Extends the grammar of graphics {{content}} ] -- + New grammar classes (functions) + How a plot should change with time {{content}} --- #### Topic 3: Dynamic visualizations .tiny[ ```r # Use the gapminder dataset in ggplot ggplot(data=gapminder, aes(x=gdpPercap, y=lifeExp, size=pop, color=country)) + # Add a point geom geom_point(alpha=0.7, show.legend=FALSE) ``` ] --- #### Topic 3: Dynamic visualizations <center><img src="./images/p8a.png" alt="ggplot"</></center> --- #### Topic 3: Dynamic visualizations .tiny[ ```r # Use the gapminder dataset in ggplot ggplot(data=gapminder, aes(x=gdpPercap, y=lifeExp, size=pop, color=country)) + # Add a point geom geom_point(alpha=0.7, show.legend=FALSE) + ``` ```r # Add some manual scaling and facets scale_colour_manual(values=country_colors) + scale_size(range=c(2, 12)) + scale_x_log10() + facet_wrap(~continent, nrow=1) ``` ] --- #### Topic 3: Dynamic visualizations <center><img src="./images/p8b2.png" alt="ggplot"</></center> --- #### Topic 3: Dynamic visualizations .tiny[ ```r # Use the gapminder dataset in ggplot ggplot(data=gapminder, aes(x=gdpPercap, y=lifeExp, size=pop, color=country)) + # Add a point geom geom_point(alpha=0.7, show.legend=FALSE) + ``` ```r # Add some manual scaling and facets scale_colour_manual(values=country_colors) + scale_size(range=c(2, 12)) + scale_x_log10() + facet_wrap(~continent, nrow=1) + ``` ```r # Animate figure with gganimate package transition_time(year) + ease_aes('linear') + labs(title='Year: {frame_time}', x='GDP per capita', y='Life expectancy') ``` ] --- #### Topic 3: Dynamic visualizations <center><img src="./images/gapminder.gif" width=700 alt="ggplot"</></center> --- class: center, middle, inverse #### Topic 3: Dynamic visualizations #### ggplot2 integrations <br/><br/> <br/><br/> <br/><br/> <br/><br/> <br/><br/> --- #### Topic 3: Dynamic visualizations .right-column2[ <center><img src="./images/ggplot_hex.png" height=250 alt="RStudio" </center> ] .left-column2[ * **ggplot2** integrations * Integrate with ggplot2 objects + Used as required arguments ] --- #### Topic 3: Dynamic visualizations .right-column2[ <center><img src="./images/plotly_logo.png" height=250 alt="RStudio" </center> ] .left-column2[ * **ggplot2** integrations - **plotly** * Extends the grammar of graphics + Creates interactive web graphics + Customizable user interface ] --- #### Topic 3: Dynamic visualizations .tiny2[
] --- #### Topic 3: Dynamic visualizations .tiny2.left-column5b[ * Create ggplot object ```r pacman::p_load(gapminder,plotly) plot <- ggplot(data=gapminder, aes(x=gdpPercap, y=lifeExp, size=pop, color=continent)) + # Add a point geom geom_point(alpha=0.7, show.legend=FALSE, aes(frame = year, ids = country)) + # Add some manual scaling scale_colour_manual(values=continent_colors) + scale_size(range=c(2, 12)) + scale_x_log10() + labs(x='GDP per capita', y='Life expectancy at birth') ``` ] .tiny2.right-column5b[] --- #### Topic 3: Dynamic visualizations <center><img src="./images/plotly.png" alt="ggplot"</></center> --- #### Topic 3: Dynamic visualizations .tiny2.left-column5b[ * Create ggplot object ```r pacman::p_load(gapminder,plotly) plot <- ggplot(data=gapminder, aes(x=gdpPercap, y=lifeExp, size=pop, color=continent)) + # Add a point geom geom_point(alpha=0.7, show.legend=FALSE, aes(frame = year, ids = country)) + # Add some manual scaling scale_colour_manual(values=continent_colors) + scale_size(range=c(2, 12)) + scale_x_log10() + labs(x='GDP per capita', y='Life expectancy at birth') ``` ] .tiny2.right-column5b[ * Pass to plotly ```r fig <- ggplotly(plot, height=500, width=750) %>% animation_opts( 1000, easing = "linear", redraw = FALSE) %>% animation_slider( currentvalue=list(prefix="YEAR ", font=list(color="red"), xanchor='left', yanchor='top')) %>% animation_button(x=1, xanchor="right", y=-0.2, yanchor="bottom" ) ``` ] --- #### Topic 3: Dynamic visualizations
--- #### Topic 3: Dynamic visualizations .right-column2[ <center><img src="./images/rayshader_hex.png" height=250 alt="RStudio" </center> ] .left-column2[ * **ggplot2** integrations - **rayshader** * Extends the grammar of graphics + Translates ggplot2 objects into 3D + Models can be rotated and examined interactively ] --- #### Topic 3: Dynamic visualizations <center><img src="./images/smtn_3D.png" width=700 alt="ggplot"</></center> --- #### Topic 3: Dynamic visualizations .tiny2[ * Create ggplot object with raster data (digital elevation model) ```r # Load necessary packages pacman::p_load(tidyverse, raster, ggthemes, sp, sf, rasterVis, rayshader) # Read raster dataset from url and convert to data frame url <- "https://github.com/jeremymack-LU/rviz/blob/master/data/smtn_dem_clip.tif?raw=true" r.dem <- raster::raster(url) r.df <- raster::as.data.frame(r.dem, xy=TRUE) # Drop NAs and rename raster grid cell variable r.df <- r.df %>% drop_na() %>% rename(elevation=3) # Create ggplot2 object r.plot <- ggplot() + geom_raster(data=r.df, aes(x=x, y=y, fill=elevation), interpolate=TRUE, show.legend=TRUE) + scale_fill_gradientn(name = "Elevation", colors=terrain.colors(10)) + coord_quickmap() + theme_dark(base_size=6) + theme(axis.title=element_blank(), legend.position='top', legend.direction='horizontal') ``` ] --- #### Topic 3: Dynamic visualizations <center><img src="./images/smtn_raster.png" width=700 alt="ggplot"</></center> --- #### Topic 3: Dynamic visualizations .tiny2[ * Pass ggplot object to rayshader ```r # Pass ggplot2 object to rayshader function plot_gg(r.plot, multicore=TRUE, width=5, height=5, scale=200, windowsize=c(1400,866), zoom=0.55, phi=30) # Capture snapshot of the rgl window render_snapshot(filename = "smtn_3D", clear=TRUE) ``` ] --- #### Topic 3: Dynamic visualizations <center><img src="./images/smtn_3D_2.png" width=600 alt="ggplot"</></center> --- #### Topic 3: Dynamic visualizations .tiny2.left-column7[ * Create ggplot object ```r # Load viridis package library(viridis) # Plot using mtcars dataset gg_cars <- ggplot(mtcars) + geom_point(aes(x=disp, y=mpg, color=wt), size=2) + scale_fill_viridis("Weight") + ggtitle("mtcars: Displacement vs mpg vs # of cylinders") + theme_dark() + theme(title = element_text(size=8), text = element_text(size=12), legend.position = 'bottom') ``` ] .tiny2.right-column7[ * Pass to rayshader ```r # Pass ggplot2 object to rayshader function plot_gg(gg_cars, multicore=TRUE, width=6, height=5, scale=250, windowsize=c(1400,866), zoom=0.55, phi=30) # Capture snapshot of the rgl window render_snapshot(filename = "mtcars", clear=TRUE) ``` ] --- #### Topic 3: Dynamic visualizations <center><img src="./images/mtcars.png" width=600 alt="ggplot"</></center> --- class: inverse #### Review - Dynamic visualizations: * Dynamic visualization created with **ggplot2** extensions and integrations * **gganimate** is a popular extension for animating graphs * Adds new grammar to ggplot2 * **rayshader** is a popular integration for creating 3D objects * Uses ggplot objects as required arguments <br/><br/><br/><br/> .pull-right3[ <img src="./images/rayshader.png" height=220 alt="ggplot" align="right"> <img src="./images/gganimate_hex.png" height=220 alt="RStudio" align="right" style="vertical-align:middle;margin:0px 10px"> ] --- class: center, middle, inverse #### Topic 4: Shiny Apps - a preview <br/><br/> <br/><br/> <br/><br/> <br/><br/> <br/><br/> <br/><br/> --- #### Topic 4: Shiny Apps - a preview .right-column2[ <center><img src="./images/shiny_hex.png" height=250 alt="RStudio" </center> <br/><br/> <center><img src="./images/master_shiny.jpg" height=250 alt="RStudio" </center> ] .left-column2[ * [RStudio](<https://shiny.rstudio.com/>) product - **shiny** * Build interactive web applications, reports, and dashboards with R * Reactive Programming ] --- #### Topic 4: Shiny Apps - a preview [<center><img src="./images/purpleair_shiny.png" alt="ggplot"</></center>](https://jeremymack.shinyapps.io/purpleair/) --- class: inverse #### Review - Data Visualization Resources: * RStudio (and user contributed) cheatsheets - [link](https://rstudio.com/resources/cheatsheets/) -- * Data visualization (**ggplot**) * *R for Data Science* by Hadley Wickham and Garret Grolemund - [link](https://r4ds.had.co.nz/index.html) -- * Thomas Lin Pedersen ggplot2 presentation (YouTube), Part 1 - [link](https://www.youtube.com/watch?v=h29g21z0a68) * Thomas Lin Pedersen ggplot2 presentation (YouTube), Part 2 - [link](https://www.youtube.com/watch?v=0m4yywqNPVY) -- * Shiny Apps * *Mastering Shiny* by Hadley Wickham - [link](https://mastering-shiny.org) * RStudio tutortials - [link](https://shiny.rstudio.com/tutorial/) --- class: center, middle, inverse, title-slide ## Questions? <img src="./images/contact.png" alt="RStudio" height=400/>