class: titleSlide, hide_logo # Data Visualization ## Small multiples and patchwork <br> <center><img src="data:image/png;base64,#logo.png" width="200px"/></center> --- class: left, hide_logo, hide-count ## Setup * **Option 1**: Download and unzip today's materials into `glhlth562/materials` on YOUR computer * **Option 2**: Download then upload today's materials into `glhlth562/materials` in the cloud * **Option 3**: Pull updates from [github](https://github.com/ericpgreen/glhlth562) (assuming you cloned this repo previously) --- class: left, hide-count ### Your data are probably grouped .panelset[ .panel[.panel-name[Plot] <img src="data:image/png;base64,#dataviz4_deck_files/figure-html/grouped_plot-1.png" width="90%" /> ] .panel[.panel-name[Data] ```r gapminder %>% select(country, year, gdpPercap) %>% arrange(year) ``` ``` ## # A tibble: 1,704 × 3 ## country year gdpPercap ## <fct> <int> <dbl> ## 1 Afghanistan 1952 779. ## 2 Albania 1952 1601. ## 3 Algeria 1952 2449. ## 4 Angola 1952 3521. ## 5 Argentina 1952 5911. ## 6 Australia 1952 10040. ## 7 Austria 1952 6137. ## 8 Bahrain 1952 9867. ## 9 Bangladesh 1952 684. ## 10 Belgium 1952 8343. ## # … with 1,694 more rows ``` ] .panel[.panel-name[Your Turn] How do you tell `ggplot()` that your data are grouped? ```r # see chunk 'grouped' ggplot(data = gapminder, mapping = aes(x = year, y = gdpPercap)) + geom_line() ``` ] ] --- class: left, hide-count ### You probably need more than one plot .panelset[ .panel[.panel-name[Code] ```r # see chunk 'need' ggplot(data = gapminder, mapping = aes(x = year, y = gdpPercap)) + geom_line(aes(group = country)) ``` ] .panel[.panel-name[Plot] <img src="data:image/png;base64,#dataviz4_deck_files/figure-html/need-plot-1.png" width="90%" /> ] .panel[.panel-name[Your Turn] What happens if you use the `color` argument instead of `group`? ] ] --- class: left, hide-count background-image: url("data:image/png;base64,#img/smurfs.png") --- class: left, hide-count ### `facet_wrap()` .panelset[ .panel[.panel-name[Code] ```r # see chunk 'wrap' ggplot(gapminder, aes(x = year, y = gdpPercap)) + geom_line(color="gray70", aes(group = country)) + geom_smooth(size = 1.1, method = "loess", se = FALSE) + scale_y_log10(labels=scales::dollar) + facet_wrap(~ continent, ncol = 5) + # HERE!! labs(x = "Year", y = "GDP per capita", title = "GDP per capita on Five Continents") ``` ] .panel[.panel-name[Plot] <img src="data:image/png;base64,#dataviz4_deck_files/figure-html/wrap-plot-1.png" width="90%" /> ] .panel[.panel-name[Your Turn] * Change `ncol = 5` to `ncol = 4`, run, and then replace it with `nrow = 5` * Replace `continent` in `facet_wrap(~ continent, ncol = 5)` with `country` ] ] --- class: left, hide-count ### `facet_grid()` .panelset[ .panel[.panel-name[Code] ```r # see chunk 'grid' ggplot(gss_sm, aes(x = age, y = childs)) + geom_point(alpha = 0.2) + geom_smooth() + facet_grid(sex ~ race) ``` ] .panel[.panel-name[Plot] <img src="data:image/png;base64,#dataviz4_deck_files/figure-html/grid-plot-1.png" width="90%" /> ] .panel[.panel-name[Your Turn] * Swap `sex` and `race` in the `facet_grid()` formula * Change the `alpha` value * Comment out `geom_smooth()` * Add `size=5` to `geom_point()` ] ] --- class: left, hide_logo, hide-count ### Arrange multiple plots with patchwork <iframe src="https://patchwork.data-imaginist.com/" width="100%" height="90%" data-external="1"></iframe> --- class: left, hide-count ### Patchwork .panelset[ .panel[.panel-name[Code] ```r # see chunk 'patch' p1 <- ggplot(data = penguins, aes(x = flipper_length_mm)) + geom_histogram(aes(fill = species), alpha = 0.5, position = "identity") + scale_fill_manual(values = c("darkorange","purple","cyan4")) + theme_minimal() + labs(x = "Flipper length (mm)", y = "Frequency", title = "Plot 1") p2 <- ggplot(data = penguins, aes(x=species, y = flipper_length_mm, fill=species)) + geom_violin() + scale_fill_manual(values = c("darkorange","purple","cyan4")) + theme_minimal() + labs(y = "Flipper length (mm)", x = NULL, title = "Plot 2") + theme(legend.position = "none") p3 <- ggplot(penguins, aes(x = species)) + geom_bar(alpha = 0.8) + theme_minimal() + labs(title = "Plot 3") p4 <- penguins %>% remove_missing() %>% group_by(species) %>% summarise(mean_bmg = mean(body_mass_g)) %>% ggplot() + geom_segment(aes(x = 0, xend = mean_bmg, y = reorder(species, mean_bmg), yend = reorder(species, mean_bmg)), color = "grey", size = 2) + geom_point(aes(y = reorder(species, mean_bmg), x = mean_bmg), size = 5, color = "darkorange") + labs(x = NULL, y = NULL, title = "Plot 4", subtitle = "Average body mass (g) by species") + theme_minimal() + theme(plot.title = element_text(face="bold"), plot.title.position = "plot") ``` ] .panel[.panel-name[Plot] ```r (p1 + p2) / (p3 + p4) ``` <img src="data:image/png;base64,#dataviz4_deck_files/figure-html/patch-plot-1.png" width="70%" /> ] .panel[.panel-name[Your Turn] Review the layout options at [https://tinyurl.com/patchopt](https://tinyurl.com/patchopt) and experiment with new configurations and adding annotations ] ] --- class: left # Credits Deck by Eric Green ([@ericpgreen](https://twitter.com/ericpgreen)), licensed under Creative Commons Attribution [CC BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/) * {[`xaringan`](https://github.com/yihui/xaringan)} for slides with help from {[`xaringanExtra`](https://github.com/gadenbuie/xaringanExtra)}