class: center, middle, inverse, title-slide .title[ # Extensions & Interactivity ] --- class: inverse ## Outline <br> ### 1. Composing multiple plots ### 2. A plethora of `{ggplot2}` extenstions ### 3. Adding interactivity --- ## Setup ```r ## devtools::install_github("haleyjeppson/NCME23data") library(NCME23data) data(pisa_usa) data(pisa_small) data(pisa_wide) ``` --- class: inverse, center background-image: url(https://pbs.twimg.com/media/EStP8qNU0AASPU-?format=jpg&name=4096x4096) background-size: 750px background-position: 50% 60% # Compose multiple plots <br/><br/><br/><br/><br/><br/><br/><br/><br/> <br/><br/><br/><br/><br/><br/><br/><br/><br/> <p style="font-size:15px;text-align:center;">Artwork by <a href="https://twitter.com/allison_horst?ref_src=twsrc%5Egoogle%7Ctwcamp%5Eserp%7Ctwgr%5Eauthor">@allison_horst</a></p> --- ## Patchwork First we will build multiple related plots. Here's the first: .left-code[ ```r p1 <- ggplot(pisa_small) + geom_violin( aes( x = math, y = region, color = region, fill = region), alpha = 0.8 ) + theme_light() + ggokabeito::scale_fill_okabe_ito() + ggokabeito::scale_color_okabe_ito() + labs( x = "2015 PISA Math score", y = NULL ) *p1 ``` ] .right-plot[ <img src="extensions_files/figure-html/unnamed-chunk-2-1.png" width="100%" /> ] --- ## Patchwork Here's the second .... .left-code[ ```r p2 <- ggplot(pisa_small) + geom_bar( aes( y = region, fill = region ), alpha = 0.8 ) + theme_light() + ggokabeito::scale_fill_okabe_ito() + ggokabeito::scale_color_okabe_ito() + labs( x = "Number of students", y = NULL ) *p2 ``` ] .right-plot[ <img src="extensions_files/figure-html/unnamed-chunk-3-1.png" width="100%" /> ] --- ## Patchwork ... and here's the third. .left-code[ ```r p3 <- ggplot(pisa_wide, aes(x = Female, y = Male)) + geom_abline( alpha = 0.2, linetype = "dashed" ) + geom_point( aes(color = region), alpha = 0.9, size = 2.5 ) + theme_light() + ggokabeito::scale_fill_okabe_ito() + ggokabeito::scale_color_okabe_ito() + labs(title = "Gender Gaps in PISA 2015 Math Scores", x = "girls math score", y = "boys math score") *p3 ``` ] .right-plot[ <img src="extensions_files/figure-html/unnamed-chunk-4-1.png" width="100%" /> ] --- ## Patchwork Use `+` and `/` to put multiple figures together into a single graphic .left-code[ ```r library(patchwork) *(p1 | p2) / p3 ``` ] .right-plot[ <img src="extensions_files/figure-html/unnamed-chunk-5-1.png" width="100%" /> ] --- ## Patchwork Use `plot_layout()` to control layout .left-code[ ```r (p1 | p2) / p3 + plot_layout( * heights = c(1,2), * guides = 'collect' ) ``` ] .right-plot[ <img src="extensions_files/figure-html/unnamed-chunk-6-1.png" width="100%" /> ] --- ## Patchwork Use `&` to apply theme throughout .left-code[ ```r (p1 | p2) / p3 + plot_layout( heights = c(1,2) * ) & theme( legend.position = 'none' ) ``` ] .right-plot[ <img src="extensions_files/figure-html/unnamed-chunk-7-1.png" width="100%" /> ] --- ## Patchwork Use `plot_annotation()` to add titles and tags .left-code[ ```r (p1 | p2) / p3 + plot_layout( heights = c(1,2) ) + plot_annotation( * title = 'Pisa Math Scores in 2015', * subtitle = 'A subset of the data shown by region and sex', * tag_levels = 'A' ) & theme( legend.position = 'none' ) ``` ] .right-plot[ <img src="extensions_files/figure-html/unnamed-chunk-8-1.png" width="100%" /> ] --- class: inverse, center # `{ggplot2}` extensions <img src="images/gganimate.gif" width = "90%"> --- ## `{ggplot2}` extension packages .pull-left[ ### Theming & Compositions - [`{cowplot}`](https://cran.r-project.org/web/packages/cowplot/vignettes/introduction.html) - [`{ggthemes}`](https://yutannihilation.github.io/allYourFigureAreBelongToUs/ggthemes/) - [`{ggrepel}`](https://github.com/slowkow/ggrepel) - [`{ggtext}`](https://wilkelab.org/ggtext/) ### Animation & Interactivity - [`{gganimate}`](https://gganimate.com/) - [`{ggigraph}`](https://github.com/davidgohel/ggiraph) - [`{plotly}`](https://plotly.com/r/getting-started/) ] .pull-right[ ### Additional plot types - [`{ggpubr}`](https://rpkgs.datanovia.com/ggpubr/) - [`{GGally}`](https://ggobi.github.io/ggally/) - [`{ggcorrplot}`](http://www.sthda.com/english/wiki/ggcorrplot-visualization-of-a-correlation-matrix-using-ggplot2) - [`{ggstatsplot}`](https://github.com/IndrajeetPatil/ggstatsplot) - [`{ggdag}`](https://github.com/r-causal/ggdag) - [`{ggradar}`](https://github.com/ricardo-bion/ggradar) ] .footnote[ ### See [gallery](https://exts.ggplot2.tidyverse.org/gallery/) for more! ] --- ## ggrepel .left-code[ ```r ggplot(pisa_wide, aes(x = Female, y = Male)) + geom_abline( alpha = 0.2, linetype = "dashed" ) + geom_point( aes(color = region), alpha = 0.9, size = 2.5 ) + * geom_text( aes(label = country) ) + theme_light() + ggokabeito::scale_fill_okabe_ito() + ggokabeito::scale_color_okabe_ito() + labs( title = "Gender Gaps in PISA 2015 Math Scores", x = "girls math score", y = "boys math score" ) ``` ] .right-plot[ <img src="extensions_files/figure-html/unnamed-chunk-9-1.png" width="100%" /> ] --- ## ggrepel .left-code[ ```r ggplot(pisa_wide, aes(x = Female, y = Male)) + geom_abline( alpha = 0.2, linetype = "dashed" ) + geom_point( aes(color = region), alpha = 0.9, size = 2.5 ) + * ggrepel::geom_text_repel( aes(label = country) ) + theme_light() + ggokabeito::scale_fill_okabe_ito() + ggokabeito::scale_color_okabe_ito() + labs( title = "Gender Gaps in PISA 2015 Math Scores", x = "girls math score", y = "boys math score" ) ``` ] .right-plot[ <img src="extensions_files/figure-html/unnamed-chunk-10-1.png" width="100%" /> ] --- ## ggpubr Grouped scatter plot with marginal density plots .left-code[ ```r library(ggpubr) ggscatterhist(pisa_small, x = "math", y = "reading", color = "OECD", size = 3, alpha = 0.6, palette = c("#3C5488", "#E64B35"), margin.params = list( fill = "OECD", color = "black", size = 0.2) ) ``` ] .right-plot[ <img src="extensions_files/figure-html/unnamed-chunk-11-1.png" width="100%" /> ] --- ## GGally::ggpairs() .left-code[ ```r library(GGally) pisa_small_subset <- pisa_small %>% select(OECD, wealth, escs_index, test_anxiety, math:science) GGally::ggpairs( pisa_small_subset, aes(color = OECD, alpha = 0.5 ) ) + theme_bw() + scale_color_manual( values = c("#3C5488", "#E64B35") ) + scale_fill_manual( values = c("#3C5488", "#E64B35") ) ``` ] .right-plot[ <img src="extensions_files/figure-html/unnamed-chunk-12-1.png" width="100%" /> ] --- ## ggcorrplot() .left-code[ ```r # devtools::install_github("kassambara/ggcorrplot") library(ggcorrplot) pisa_small_subset <- pisa_small %>% select( wealth, escs_index, test_anxiety:parent_support_emotional, teacher_support_science:science) cor_pisa <- cor(pisa_small_subset, use = "complete.obs") ggcorrplot(cor_pisa, type = "lower", outline.col = "white", ggtheme = ggplot2::theme_light, colors = c( "mediumpurple", "#EEEEEE", "orange"), lab = TRUE ) ``` ] .right-plot[ <img src="extensions_files/figure-html/unnamed-chunk-13-1.png" width="100%" /> ] --- ## ggstatsplot `ggbetweenstats()` creates either a violin plot, a box plot, or a mix of two for between-group or between-condition comparisons with results from statistical tests in the subtitle .left-code[ ```r # install.packages("ggstatsplot") library(ggstatsplot) set.seed(123) ggbetweenstats( data = pisa_small, x = OECD, y = math, title = "Distribution of math scores by OECD status" ) ``` ] .right-plot[ <img src="extensions_files/figure-html/unnamed-chunk-14-1.png" width="100%" /> ] --- class: yourturn .center[ ## Your Turn ] ### Look through the [extensions gallery](https://exts.ggplot2.tidyverse.org/gallery/). Pick out a package and install it. ### **Bonus**: try to run the given example! --- class: inverse, center, middle # Interactivity --- ## ggplotly .left-code[ ```r library(plotly) *ggplotly(p3) ``` ] .right-plot[
] --- ## ggplotly Modify the tooltip output .left-code[ ```r p3_updated <- ggplot(pisa_wide, aes(x = Female, y = Male, * text = paste("country:", country) )) + geom_abline(alpha = 0.2, linetype = "dashed") + geom_point( aes(color = region), alpha = 0.9, size = 2.5 ) + theme_light() + ggokabeito::scale_fill_okabe_ito() + ggokabeito::scale_color_okabe_ito() + labs(title = "Gender Gaps in PISA 2015 Math Scores", x = "girls math score", y = "boys math score") ggplotly(p3_updated) ``` ] .right-plot[
] --- ## ggplotly Modify the tooltip output .left-code[ ```r ggplotly(p3_updated, * tooltip = c("text", "x", "y") ) ``` ] .right-plot[
] --- ## ggiraph .left-code[ ```r library(ggiraph) p3_int <- ggplot(pisa_wide, aes(x = Female, y = Male)) + geom_abline(alpha = 0.2, linetype = "dashed") + * geom_point_interactive( aes(color = region, * tooltip = country, * data_id = country), alpha = 0.9, size = 2.5 ) + theme_light() + ggokabeito::scale_fill_okabe_ito() + ggokabeito::scale_color_okabe_ito() + labs(title = "Gender Gaps in PISA 2015 Math Scores", x = "girls math score", y = "boys math score") girafe(ggobj = p3_int) ``` ] .right-plot[
] --- <!-- ## ggdag --> class: yourturn .center[ ## Your Turn ] ### Take any plot from today's examples and add interactivity with `ggplotly()` (**Bonus**: modify the toolstip) --- ## Resources - Documentation: http://ggplot2.tidyverse.org/reference/ - RStudio cheat sheet for [ggplot2](https://posit.co/wp-content/uploads/2022/10/data-visualization-1.pdf) - Sam Tyner's [ggplot2 workshop](https://sctyner.github.io/user20-proposal.html) - Thomas Lin Pedersen's ggplot2 webinar: [part 1](https://youtu.be/h29g21z0a68) and [part 2](https://youtu.be/0m4yywqNPVY) - Cedric Scherer's ["A ggplot2 tutorial for beautiful plotting in R"](https://www.cedricscherer.com/2019/08/05/a-ggplot2-tutorial-for-beautiful-plotting-in-r/#legends)