class: center, middle, inverse, title-slide # Introduction to the Patchwork Package ## Scaffolding for Composite Figures and Infographics ### Melinda Higgins ### Atlanta R Users 06/24/2021 --- 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-code[ # Make a Scatterplot Let's look at the Palmer Penguins dataset again. 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") ``` ] .right-plot[ <img src="MHiggins_IntroToPatchwork.v2_files/figure-html/plot-label-out-1.png" width="100%" /> ] --- .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="MHiggins_IntroToPatchwork.v2_files/figure-html/plot-label1-out-1.png" width="100%" /> ] --- .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="MHiggins_IntroToPatchwork.v2_files/figure-html/plot-label2-out-1.png" width="100%" /> ] --- .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="MHiggins_IntroToPatchwork.v2_files/figure-html/plot-label3-out-1.png" width="100%" /> ] --- .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="MHiggins_IntroToPatchwork.v2_files/figure-html/plot-label4-out-1.png" width="100%" /> ] --- .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="MHiggins_IntroToPatchwork.v2_files/figure-html/plot-label5-out-1.png" width="100%" /> ] --- class: center, middle, inverse # Great Resources for R Graphics <hr> ## R Graphics Cookbook ### [https://r-graphics.org/](https://r-graphics.org/) ### [http://www.cookbook-r.com/Graphs/](http://www.cookbook-r.com/Graphs/) & ## R-Gallery ### [https://www.r-graph-gallery.com/](https://www.r-graph-gallery.com/) --- .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="MHiggins_IntroToPatchwork.v2_files/figure-html/histplot-out-1.png" width="90%" /> ] --- # 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="MHiggins_IntroToPatchwork.v2_files/figure-html/histplot2-out-1.png" width="100%" /> ] --- class: center, middle, inverse # Pulling it together - make a canvas # Patchwork! <img src="img/patchworklogo.png" width="20%" /> .footnote[ Learn more at [https://patchwork.data-imaginist.com/](https://patchwork.data-imaginist.com/) and fun R-Bloggers post [https://www.r-bloggers.com/2020/03/patchwork-r-package-goes-nerd-viral/](https://www.r-bloggers.com/2020/03/patchwork-r-package-goes-nerd-viral/) ] --- <img src="img/patchwork_blank.png" width="80%" /> .footnote[ * Illustrations by Allison Horst, RStudio Artist in Residence, <https://github.com/allisonhorst/stats-illustrations> ] --- ## Saving plots as objects .left-code[ ```r # save a plot as an object p1 <- ggplot(penguins, aes(x=bill_length_mm, y=bill_depth_mm, color = species)) + geom_point() # plot the object *p1 ``` ] .right-plot[ <!-- --> ] --- ## Side by Side - use syntax `p1 | p2` .left-code[ ```r library(ggplot2) library(patchwork) # create another plot object p2 <- ggplot(penguins, aes(x=species, y=bill_length_mm)) + geom_boxplot() + xlab("Species") + ylab("Bill Depth (mm)") # arrange the plot objects *(p1 | p2) ``` ] .right-plot[ <!-- --> ] --- ## Stack 2 Plots - use syntax `p2 / p3` .left-code[ ```r # make another plot 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 top / bottom *p2 / p3 ``` ] .right-plot[ <!-- --> ] --- ## Combine - syntax `(p1 | p2)/p3` .left-code[ ```r # combine syntax # arrange plots as you like *(p1 | p2) / p3 ``` ] .right-plot[ <!-- --> ] --- class: center, middle, inverse # Thank you! <hr> # Melinda Higgins ### Materials for this presentation [https://melindahiggins2000.github.io/AtlantaRUsers_24June2021/](https://melindahiggins2000.github.io/AtlantaRUsers_24June2021/) ### More about me [https://melindahiggins.com/](https://melindahiggins.com/)