class: center, middle, inverse, title-slide .title[ # Module 03: Introduction to
ggplot2
] .subtitle[ ## Rollins COVID-19 Epidemiology Fellowship R Training: Nov 11, 2022 ] .author[ ###
Melinda Higgins
] .date[ ### Director Biostatistics & Data Core School of Nursing - Emory University ] --- background-size: 80% background-image: url(img/ggplot2_masterpiece.png) background-position: 70% 70% class: center, top .footnote[ * Illustrations by Allison Horst, RStudio Artist in Residence, <https://github.com/allisonhorst/stats-illustrations> ] --- class: center, middle, inverse # Be an Artist! # Create Awesome Figures with `ggplot2` --- .left-column[ # ggfree package ] .right-column[ <img src="img/ggfree.jpg" width="80%" /> ] .footnote[[https://github.com/ArtPoon/ggfree](https://github.com/ArtPoon/ggfree)] --- .left-column[ # Make Art - Mandalas! ] .right-column[ <img src="img/mancalas.jpg" width="75%" /> ] .footnote[[https://www.r-craft.org/r-news/mandalas-colored/](https://www.r-craft.org/r-news/mandalas-colored/)] --- .pull-left[ ```r library(kandinsky) kandinsky(iris) ``` <!-- --> ] -- .pull-right[ ```r kandinsky(airquality) ``` <!-- --> ] .footnote[[Link to Kandinsky Package from Github](http://giorasimchoni.com/2017/07/30/2017-07-30-data-paintings-the-kandinsky-package/)] --- # Get inspired at R-Graph-Gallery .pull-left[ <img src="img/rgraphgallery_parallel.JPG" width="100%" /> ] .pull-right[ <img src="img/rgraphgallery_boxplotjitter.JPG" width="100%" /> ] .footnote[[https://www.r-graph-gallery.com/ggplot2-package.html](https://www.r-graph-gallery.com/ggplot2-package.html)] --- # R-Graph-Gallery <img src="img/rgraphgallery_violinboxplot.JPG" width="80%" /> .footnote[[https://www.r-graph-gallery.com/ggplot2-package.html](https://www.r-graph-gallery.com/ggplot2-package.html)] --- # R-Graph-Gallery - lollipops and roses <img src="img/rgraphgallery_lollipoprose.JPG" width="100%" /> .footnote[[https://www.r-graph-gallery.com/ggplot2-package.html](https://www.r-graph-gallery.com/ggplot2-package.html)] --- .left-code[ # Make a Scatterplot Let's look at the Palmer Penguins dataset. Make a scatterplot of ```r library(palmerpenguins) # Make a scatterplot ggplot(penguins) + aes(x=bill_length_mm, y=bill_depth_mm, color = species) + geom_point() + xlab("Bill Length (mm)") + ylab("Bill Depth (mm)") + ggtitle("Penguins Bill Dimensions") ``` _See "module03_Rscript.R" - EXERCISE 01)_ ] .right-plot[ <img src="Rworkshop_11Nov2022_Module03_files/figure-html/plot-label-out-1.png" width="80%" /> ] --- .left-code[ # Scatterplot - make plot area ```r # Step 1 specify dataset # penguins for ggplot() *ggplot(penguins) ``` The basic plot space is created. ] .right-plot[ <img src="Rworkshop_11Nov2022_Module03_files/figure-html/plot-label1-out-1.png" width="80%" /> ] --- .left-code[ # Scatterplot - add aethetics ```r # Next add aes (aesthetics) ggplot(penguins) + * aes(x=bill_length_mm, * y=bill_depth_mm) ``` Add "aesthetics" using aes - provide the variables for X and Y axes. Notice the `aes()` is added using the `+` operator. ] .right-plot[ <img src="Rworkshop_11Nov2022_Module03_files/figure-html/plot-label2-out-1.png" width="80%" /> ] --- .left-code[ # Add a `geom_xxx` - Geometric Object ```r # Add points to graph ggplot(penguins) + aes(x=bill_length_mm, y=bill_depth_mm) + * geom_point() ``` Add `geom_point()` to add the **points** to the graph. ] .right-plot[ <img src="Rworkshop_11Nov2022_Module03_files/figure-html/plot-label3-out-1.png" width="80%" /> ] --- .left-code[ # Add Color - use `aes()` ```r # Add color aesthetic ggplot(penguins) + aes(x=bill_length_mm, y=bill_depth_mm, * color = species) + geom_point() ``` Add `color` option to the aesthetics inside the `aes()`. ] .right-plot[ <img src="Rworkshop_11Nov2022_Module03_files/figure-html/plot-label4-out-1.png" width="80%" /> ] --- .left-code[ # Add Labels - to the axes and add a title ```r # Add axis labels and a title ggplot(penguins) + aes(x=bill_length_mm, y=bill_depth_mm, color = species) + geom_point() + * xlab("Bill Length(mm)") + * ylab("Bill Depth (mm)") + * ggtitle("Penguins Bill Dimensions") ``` Add axis labels with `xlab()` and `ylab()` and add a title with `ggtitle()`. ] .right-plot[ <img src="Rworkshop_11Nov2022_Module03_files/figure-html/plot-label5-out-1.png" width="80%" /> ] --- .footnote[See more themes at [https://ggplot2.tidyverse.org/reference/ggtheme.html](https://ggplot2.tidyverse.org/reference/ggtheme.html)] .left-code[ # Jazz it up - add a theme ```r # Add axis labels and a title ggplot(penguins) + aes(x=bill_length_mm, y=bill_depth_mm, color = species) + geom_point() + xlab("Bill Length(mm)") + ylab("Bill Depth (mm)") + ggtitle("Penguins Bill Dimensions") + * theme_dark() ``` ] .right-plot[ <img src="Rworkshop_11Nov2022_Module03_files/figure-html/plot-label6-out-1.png" width="80%" /> ] --- .footnote[Try `ggthemes` on CRAN [https://cran.r-project.org/web/packages/ggthemes/index.html](https://cran.r-project.org/web/packages/ggthemes/index.html)] .left-code[ ### `ggthemes` package ```r library(ggthemes) ggplot(penguins) + aes(x=bill_length_mm, y=bill_depth_mm, color = species) + geom_point() + xlab("Bill Length(mm)") + ylab("Bill Depth (mm)") + ggtitle("WSJ Theme") + theme_wsj() ``` ] .right-plot[ <img src="Rworkshop_11Nov2022_Module03_files/figure-html/ggtheme1-out-1.png" width="80%" /> ] --- .footnote[See `ggthemr` in development on Github [https://github.com/Mikata-Project/ggthemr](https://github.com/Mikata-Project/ggthemr)] .left-code[ ### `ggthemr` package ```r library(ggthemr) ggthemr('pale') ggplot(penguins, aes(x=bill_length_mm, y=bill_depth_mm, colour = species)) + geom_point() + xlab("Bill Length(mm)") + ylab("Bill Depth (mm)") + ggtitle("ggthemr - pale theme") + scale_colour_ggthemr_d() ggthemr_reset() ``` ] .right-plot[ <img src="Rworkshop_11Nov2022_Module03_files/figure-html/ggthemr1-out-1.png" width="80%" /> ] --- class: center, middle, inverse # A Great Resource - R Graphics Cookbook ### [https://r-graphics.org/](https://r-graphics.org/) and ### [http://www.cookbook-r.com/Graphs/](http://www.cookbook-r.com/Graphs/) --- .footnote[See code examples at [http://www.cookbook-r.com/Graphs/Plotting_distributions_(ggplot2)/](http://www.cookbook-r.com/Graphs/Plotting_distributions_(ggplot2)/)] .left-code[ # Make a histogram with overlaid density curve ```r # Look at flipper_length_mm # for Palmer Penguins # Histogram with density curve # Use y=..density.. # Overlay with transparent density plot ggplot(penguins, aes(x=flipper_length_mm)) + geom_histogram(aes(y=..density..), binwidth=2, colour="black", fill="white") + geom_density(alpha=.2, fill="#FF6666") ``` ] .right-plot[ <img src="Rworkshop_11Nov2022_Module03_files/figure-html/histplot-out-1.png" width="90%" /> ] --- class: left, middle, inverse # YOUR TURN [ZOOM BREAKOUT 10 MIN] ### 1. Open module03_Rscript.R ### 2. Do EXERCISE 02 ### 3. Do EXERCISE 03 --- # Break out by Facet/Panels .left-code[ ```r # Look at flipper_length_mm # for Palmer Penguins # Histogram with density curve # Use y=..density.. # Overlay with transparent density plot ggplot(penguins, aes(x=flipper_length_mm)) + geom_histogram(aes(y=..density..), binwidth=2, colour="black", fill="white") + geom_density(alpha=.2, fill="#FF6666") + * facet_wrap(vars(species)) ``` Add `facet_wrap()` to make histogram plots for each penguin species in a different facet or panel. ] .right-plot[ <img src="Rworkshop_11Nov2022_Module03_files/figure-html/histplot2-out-1.png" width="100%" /> ] --- # Add best fit line to scatterplot .left-code[ ```r # take out color=species ggplot(penguins, aes(x=bill_length_mm, * y=bill_depth_mm)) + geom_point() + # add best fit line # using lm, linear model geom_smooth(method=lm) + xlab("Bill Length(mm)") + ylab("Bill Depth (mm)") + ggtitle("Penguins Bill Depth by Length") ``` ] .right-plot[ <img src="Rworkshop_11Nov2022_Module03_files/figure-html/scatter01-out-1.png" width="100%" /> ] --- # Add `color` back, see how fit lines change .left-code[ ```r ggplot(penguins, aes(x=bill_length_mm, y=bill_depth_mm, * color = species)) + geom_point() + geom_smooth(method=lm) + # add best fit line # using lm, linear model xlab("Bill Length(mm)") + ylab("Bill Depth (mm)") + ggtitle("Penguins Bill Depth by Length") ``` ] .right-plot[ <img src="Rworkshop_11Nov2022_Module03_files/figure-html/scatter02-out-1.png" width="100%" /> ] --- class: left, middle, inverse # YOUR TURN [ZOOM BREAKOUT 5-10 MIN] ### 1. Open module03_Rscript.R ### 2. Do EXERCISE 04 ### 3. Do EXERCISE 05 --- # Make side-by-side boxplots .left-code[ ```r # Do boxplots of body_mass_g # by year as a factor ggplot(penguins, aes(x=as.factor(year), y=body_mass_g)) + geom_boxplot() + xlab("Year") + ylab("Body Mass (g)") + ggtitle("Body Mass by Year") ``` ] .right-plot[ <img src="Rworkshop_11Nov2022_Module03_files/figure-html/boxplot01-out-1.png" width="100%" /> ] --- # Add points over boxplots with jitter .footnote[Get Ideas at R-Gallery [https://r-graph-gallery.com/89-box-and-scatter-plot-with-ggplot2.html](https://r-graph-gallery.com/89-box-and-scatter-plot-with-ggplot2.html)] .left-code[ ```r # Do boxplots of body_mass_g # by year as a factor # add jitter'ed points ggplot(penguins, aes(x=as.factor(year), y=body_mass_g)) + geom_boxplot() + geom_jitter(color="black") + xlab("Year") + ylab("Body Mass (g)") + ggtitle("Body Mass by Year") ``` ] .right-plot[ <img src="Rworkshop_11Nov2022_Module03_files/figure-html/boxplot02-out-1.png" width="100%" /> ] --- class: left, middle, inverse # YOUR TURN [ZOOM BREAKOUT 5-10 MIN] ### 1. Open module03_Rscript.R ### 2. Do EXERCISE 06 ### 3. Do EXERCISE 07 --- class: center, middle, inverse # Pulling it together - make a canvas # Patchwork! <img src="img/patchworklogo.png" width="20%" /> --- <img src="img/patchwork_blank.png" width="80%" /> --- .left-code[ ```r library(ggplot2) library(patchwork) # save each plot as an object p1 <- ggplot(penguins, aes(x=bill_length_mm, y=bill_depth_mm, color = species)) + geom_point() p2 <- ggplot(penguins, aes(x=species, y=bill_length_mm)) + geom_boxplot() + xlab("Species") + ylab("Bill Depth (mm)") p3 <- ggplot(penguins, aes(x=flipper_length_mm)) + geom_histogram(aes(y=..density..), binwidth=2, colour="black", fill="white") + geom_density(alpha=.2, fill="#FF6666") + facet_wrap(vars(species)) # arrange plots as you like (p1 | p2) / p3 ``` ] .right-plot[ <!-- --> ] --- class: left, middle, inverse # YOUR TURN [ZOOM BREAKOUT 5 MIN] ### 1. Open module03_Rscript.R ### 2. Do EXERCISE 08