class: middle, right, title-slide # R for visualisation ## - the ggplot2 way - ### Athanasia Monika Mowinckel ### 12.04.2021 --- class: middle .pull-left[ <img src="https://www.sv.uio.no/psi/personer/vit/athanasm/athanasia.mowinckel.2.small.png" width="auto" height="600px" /> ] .pull-right[ ## Athanasia Monika Mowinckel - Staff scientist - PhD in cognitive psychology - Software Carpentry Instructor - Currently doing quite some R-package development & other in-house research software development **Find me** - Twitter: [DrMowinckels](https://twitter.com/DrMowinckels) - GitHub: [Athanasiamo](https://github.com/Athanasiamo) - Website/Blog: [DrMowinckels.io](https://drmowinckels.io/) ] --- background-image: url(https://www.lifebrain.uio.no/vrtx/decorating/resources/images/logo.png), url(https://www.lifebrain.uio.no/web-banner_rev.jpg), url(https://www.lifebrain.uio.no/vrtx/decorating/resources/images/eu-flag.jpg) background-position: 50% 10%, 60% 100%, 90% 10% background-size: 50%, 100%, 10% --- layout: true <div class="my-sidebar"></div> --- ## Graphics systems in R - ### base R - ### [plotly](https://plotly.com/r/) - ### [r2d3](https://rstudio.github.io/r2d3/) - ### [lattice](http://lattice.r-forge.r-project.org/Vignettes/src/lattice-intro/lattice-intro.pdf) --- class: dark, bottom, center background-image: url(img/ggplot2.png) background-size: 30% background-position: 50% 40% name: ggplot # Grammar of graphics --- background-image: url(img/ggplot2.png), url(https://www.bna.org.uk/media/resources/images/BNA2021_online_website_sm.png) background-size: 8%, 15% background-position: 95% 5%, 75% 5% ## Overview .pull-left[ ### Components - `ggplot()` - initiate a plot, call once - `geom_x()` - add "geometry", call several times - `scale_x()` - scale aesthetics, call several times - `coord_x()` - alter coordinate system or "zoom" in/out, call once - `theme_x()` - alter general plot aesthetics, call maximum twice - `labs()` - alter labels (some overlap with `scale_x`), call once - `facet_x()` - create grids of subplots, call once ] .pull-right[ ### Key concepts - **Mapping** - Using variables from data - Always inside an `aes()` - **Setting** - Setting a single value for all data - Never inside an `aes()` ] --- background-image: url(https://www.bna.org.uk/media/resources/images/BNA2021_online_website_sm.png) background-size: 15% background-position: 75% 5% ## Base-r `plot()` .pull-left[ **Pros** - versatile (can plot data, model outputs etc.) - quick - no extra dependencies (works out of the box) **Cons** - not very pretty - is not stored as an R 'object' - somewhat tricky to customise ] .pull-right[ ```r plot(df_long$age.category, df_long$ffa) ``` <img src="index_files/figure-html/base-plot-1.png" width="504" /> ] --- background-image: url(img/ggplot2.png), url(https://www.bna.org.uk/media/resources/images/BNA2021_online_website_sm.png) background-size: 8%, 15% background-position: 95% 5%, 75% 5% ## general build-up .pull-left[ ```r ggplot(data = df_long) ``` ] .pull-right[ <img src="index_files/figure-html/build0-rend-1.png" width="504" /> ] ??? These next slides I will run through really fast. Don't worry about catching the code, we will be going through it together shortly. I just want to leaf through the process of constructing a complex plot, before we start doing it step by step together. So don't worry that its going fast, I promise to get you on board more slowly soon. --- background-image: url(img/ggplot2.png), url(https://www.bna.org.uk/media/resources/images/BNA2021_online_website_sm.png) background-size: 8%, 15% background-position: 95% 5%, 75% 5% ## general build-up .pull-left[ ```r ggplot(data = df_long, mapping = aes(x = age.category, y = ffa)) ``` ] .pull-right[ <img src="index_files/figure-html/build1-rend-1.png" width="504" /> ] --- background-image: url(img/ggplot2.png), url(https://www.bna.org.uk/media/resources/images/BNA2021_online_website_sm.png) background-size: 8%, 15% background-position: 95% 5%, 75% 5% ## general build-up .pull-left[ ```r ggplot(data = df_long, mapping = aes(x = age.category, y = ffa)) + * geom_boxplot() ``` ] .pull-right[ <img src="index_files/figure-html/build2-rend-1.png" width="504" /> ] --- background-image: url(img/ggplot2.png), url(https://www.bna.org.uk/media/resources/images/BNA2021_online_website_sm.png) background-size: 8%, 15% background-position: 95% 5%, 75% 5% ## general build-up .pull-left[ ```r ggplot(data = df_long, mapping = aes(x = age.category, y = ffa)) + * geom_boxplot(aes(fill = age.category)) ``` ] .pull-right[ <img src="index_files/figure-html/build3-rend-1.png" width="504" /> ] --- background-image: url(img/ggplot2.png), url(https://www.bna.org.uk/media/resources/images/BNA2021_online_website_sm.png) background-size: 8%, 15% background-position: 95% 5%, 75% 5% ## general build-up .pull-left[ ```r ggplot(data = df_long, mapping = aes(x = age.category, y = ffa)) + geom_boxplot(aes(fill = age.category)) + * scale_fill_brewer(palette = "Dark2") ``` ] .pull-right[ <img src="index_files/figure-html/build4-rend-1.png" width="504" /> ] --- background-image: url(img/ggplot2.png), url(https://www.bna.org.uk/media/resources/images/BNA2021_online_website_sm.png) background-size: 8%, 15% background-position: 95% 5%, 75% 5% ## general build-up .pull-left[ ```r ggplot(data = df_long, mapping = aes(x = age.category, y = ffa)) + geom_boxplot(aes(fill = age.category)) + scale_fill_brewer(palette = "Dark2") + * theme_light() ``` ] .pull-right[ <img src="index_files/figure-html/build5-rend-1.png" width="504" /> ] --- background-image: url(img/ggplot2.png), url(https://www.bna.org.uk/media/resources/images/BNA2021_online_website_sm.png) background-size: 8%, 15% background-position: 95% 5%, 75% 5% ## general build-up .pull-left[ ```r ggplot(data = df_long, mapping = aes(x = age.category, y = ffa)) + geom_boxplot(aes(fill = age.category)) + scale_fill_brewer(palette = "Dark2") + theme_light() + * labs( * title = "FFA activation across age categories", * subtitle = "distributions of repeated participant measurements", * x = "Age category", * y = "FFA activation", * fill = "Age category") ``` ] .pull-right[ <img src="index_files/figure-html/build6-rend-1.png" width="504" /> ] --- background-image: url(img/ggplot2.png), url(https://www.bna.org.uk/media/resources/images/BNA2021_online_website_sm.png) background-size: 8%, 15% background-position: 95% 5%, 75% 5% ## general build-up .pull-left[ ```r ggplot(data = df_long, mapping = aes(x = age.category, y = ffa)) + geom_boxplot(aes(fill = age.category)) + * geom_violin(aes(fill = age.category)) + scale_fill_brewer(palette = "Dark2") + theme_light() + labs( title = "FFA activation across age categories", subtitle = "distributions of repeated participant measurements", x = "Age category", y = "FFA activation", fill = "Age category") ``` ] .pull-right[ <img src="index_files/figure-html/build7-rend-1.png" width="504" /> ] --- background-image: url(img/ggplot2.png), url(https://www.bna.org.uk/media/resources/images/BNA2021_online_website_sm.png) background-size: 8%, 15% background-position: 95% 5%, 75% 5% ## general build-up .pull-left[ ```r ggplot(data = df_long, mapping = aes(x = age.category, y = ffa)) + * geom_violin(aes(fill = age.category)) + geom_boxplot(aes(fill = age.category)) + scale_fill_brewer(palette = "Dark2") + theme_light() + labs( title = "FFA activation across age categories", subtitle = "distributions of repeated participant measurements", x = "Age category", y = "FFA activation", fill = "Age category") ``` ] .pull-right[ <img src="index_files/figure-html/build8-rend-1.png" width="504" /> ] --- background-image: url(img/ggplot2.png), url(https://www.bna.org.uk/media/resources/images/BNA2021_online_website_sm.png) background-size: 8%, 15% background-position: 95% 5%, 75% 5% ## general build-up .pull-left[ ```r ggplot(data = df_long, mapping = aes(x = age.category, y = ffa, * fill = age.category)) + * geom_violin(alpha = .5) + * geom_boxplot(alpha = .5, width = .2) + scale_fill_brewer(palette = "Dark2") + theme_light() + labs( title = "FFA activation across age categories", subtitle = "distributions of repeated participant measurements", x = "Age category", y = "FFA activation", fill = "Age category") ``` ] .pull-right[ <img src="index_files/figure-html/build9-rend-1.png" width="504" /> ] --- background-image: url(img/ggplot2.png), url(https://www.bna.org.uk/media/resources/images/BNA2021_online_website_sm.png) background-size: 8%, 15% background-position: 95% 5%, 75% 5% ## general build-up .pull-left[ ```r ggplot(data = df_long, mapping = aes(x = age.category, y = ffa, fill = age.category)) + * geom_jitter(alpha = .2, width = .2) + geom_violin(alpha = .5) + geom_boxplot(alpha = .5, width = .2) + scale_fill_brewer(palette = "Dark2") + theme_light() + labs( title = "FFA activation across age categories", subtitle = "distributions of repeated participant measurements", x = "Age category", y = "FFA activation", fill = "Age category") ``` ] .pull-right[ <img src="index_files/figure-html/build10-rend-1.png" width="504" /> ] --- background-image: url(img/ggplot2.png), url(https://www.bna.org.uk/media/resources/images/BNA2021_online_website_sm.png) background-size: 8%, 15% background-position: 95% 5%, 75% 5% ## general build-up .pull-left[ ```r ggplot(data = df_long, mapping = aes(x = age.category, y = ffa, fill = age.category)) + * geom_dotplot(binaxis = "y", * stackdir='center', * dotsize = 0.5, * alpha = .6, * binwidth = .045) + geom_violin(alpha = .5) + geom_boxplot(alpha = .5, width = .2) + scale_fill_brewer(palette = "Dark2") + theme_light() + labs( title = "FFA activation across age categories", subtitle = "distributions of repeated participant measurements", x = "Age category", y = "FFA activation", fill = "Age category") ``` ] .pull-right[ <img src="index_files/figure-html/build11-rend-1.png" width="504" /> ] --- background-image: url(img/ggplot2.png), url(https://www.bna.org.uk/media/resources/images/BNA2021_online_website_sm.png) background-size: 8%, 15% background-position: 95% 5%, 75% 5% ## general build-up .pull-left[ ```r ggplot(data = df_long, mapping = aes(x = age.category, y = ffa, fill = age.category)) + geom_dotplot(binaxis = "y", stackdir='center', dotsize = 0.5, binwidth = .045, alpha = .6) + geom_violin(alpha = .5) + geom_boxplot(alpha = .5, width = .2) + scale_fill_brewer(palette = "Dark2") + theme_light() + labs( title = "FFA activation across age categories", subtitle = "distributions of repeated participant measurements", x = "Age category", y = "FFA activation", fill = "Age category") + * facet_wrap(~face.emotion) ``` ] .pull-right[ <img src="index_files/figure-html/build12-rend-1.png" width="504" /> ] --- class: dark, middle, center ## Go to RStudio ### live coding --- class: dark, middle, center ## Bonus stuff ### shame-less promotion --- background-image: url(img/ggseg.png), url(https://www.bna.org.uk/media/resources/images/BNA2021_online_website_sm.png) background-size: 8%, 15% background-position: 95% 5%, 75% 5% ## ggseg - brain atlas plotting ```r library(ggseg) ggplot() + geom_brain(atlas = dk, position = position_brain(hemi ~ side)) ``` <center> <img src="index_files/figure-html/ggseg1-rend-1.png" width="504" /> <center> --- background-image: url(img/ggseg.png), url(https://www.bna.org.uk/media/resources/images/BNA2021_online_website_sm.png) background-size: 8%, 15% background-position: 95% 5%, 75% 5% ## ggseg - brain atlas plotting .pull-left[ ```r test_data <- dplyr::tribble( ~region, ~activation, "banksts", .5, "precentral", 1, "precuneus", 1.5, "lingual", .2 ) ggplot(test_data) + geom_brain( * aes(fill = activation), atlas = dk, position = position_brain(hemi ~ side) ) ``` ] .pull-right[ <img src="index_files/figure-html/ggseg2-rend-1.png" width="504" /> ] --- background-image: url(img/ggseg.png), url(https://www.bna.org.uk/media/resources/images/BNA2021_online_website_sm.png) background-size: 8%, 15% background-position: 95% 5%, 75% 5% ## ggseg - brain atlas plotting .pull-left[ ```r test_data <- dplyr::tribble( ~region, ~activation, "bankssts", .5, "precentral", 1, "precuneus", 1.5, "lingual", .2 ) ggplot(test_data) + geom_brain( * aes(fill = activation), atlas = dk, position = position_brain(hemi ~ side) ) + scale_fill_viridis_c(na.value = "grey20") + theme_brain2() ``` ] .pull-right[ <img src="index_files/figure-html/ggseg3-rend-1.png" width="504" /> ] --- background-image: url(img/ggseg.png), url(https://www.bna.org.uk/media/resources/images/BNA2021_online_website_sm.png) background-size: 8%, 15% background-position: 95% 5%, 75% 5% ## ggseg - brain atlas plotting .pull-left[ ```r ggseg_p <- ggplot(test_data) + geom_brain( aes(fill = activation), atlas = dk, position = position_brain(hemi ~ side), show.legend = FALSE ) + theme_brain2() bar_p <- ggplot(test_data, aes(x = region, y = activation, fill = activation)) + geom_bar(stat = "identity") + theme_light() *library(patchwork) *ggseg_p / bar_p & * scale_fill_viridis_c(na.value = "grey20") & * plot_annotation( * title = "Brain activation strength", * subtitle = "Desikan-Killiany atlas", * ) ``` ] .pull-right[ <img src="index_files/figure-html/ggseg4-rend-1.png" width="504" /> ]