Skip to content

Commit 1581851

Browse files
Make some formatting edits
1 parent 48c33e1 commit 1581851

File tree

4 files changed

+16
-18
lines changed

4 files changed

+16
-18
lines changed

2024/03-extensions/slides.qmd

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ editor: visual
1515
## Learning Objectives
1616

1717
- Understand where to find packages that extend `ggplot2`
18-
- Assemble multi-panel figures with `patchwork`
19-
- Make animated plots with `gganimate`
20-
- Visualize distributions with `ggdist`
21-
- Plot network data with `ggraph`
18+
- Demonstrate some extension packages
19+
- Assemble multi-panel figures with `patchwork`
20+
- Make animated plots with `gganimate`
21+
- Visualize distributions with `ggdist`
22+
- Plot network data with `ggraph`
2223

2324
## Packages
2425

@@ -119,10 +120,14 @@ Things to notice:
119120

120121
```{r}
121122
p1 <-
122-
ggplot(penguins, aes(x = body_mass_g, y = bill_length_mm, color = species)) +
123+
ggplot(penguins, aes(x = body_mass_g,
124+
y = bill_length_mm,
125+
color = species)) +
123126
geom_point()
124127
p2 <-
125-
ggplot(penguins, aes(x = bill_depth_mm, y = bill_length_mm, color = species)) +
128+
ggplot(penguins, aes(x = bill_depth_mm,
129+
y = bill_length_mm,
130+
color = species)) +
126131
geom_point()
127132
p3 <-
128133
ggplot(penguins, aes(x = body_mass_g)) +
@@ -132,24 +137,17 @@ p3 <-
132137

133138
## Combine plots
134139

135-
::: columns
136-
::: {.column width="50%"}
137-
::: nonincremental
138140
- `+` wraps plots
139141
- `|` combines plots horizontally
140142
- `/` combines plots vertically
141143
- `()` can be used to nest operations
142-
:::
143-
:::
144144

145-
::: {.column width="50%"}
145+
## Combine plots
146+
146147
```{r}
147-
#| fig-width: 5
148-
#| fig-height: 5
149148
(p1 | p2) / p3
150149
```
151-
:::
152-
:::
150+
153151

154152
## Combine guides
155153

_freeze/2024/03-extensions/slides/execute-results/html.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
2-
"hash": "e9f774d08fff260d505525bbe971bf9a",
2+
"hash": "786ee5d6418aeba3f23ef150b9e0f28c",
33
"result": {
44
"engine": "knitr",
5-
"markdown": "---\ntitle: \"Exploring the Wide World of ggplot2 Extensions\"\nauthor: \n - \"Eric R. Scott\"\n - \"Kristina Riemer\"\n - \"Renata Diaz\"\ndate: 2024-06-20\nformat: \n uaz-revealjs: default\nexecute: \n echo: true\neditor: visual\n---\n\n\n## Learning Objectives\n\n- Understand where to find packages that extend `ggplot2`\n- Assemble multi-panel figures with `patchwork`\n- Make animated plots with `gganimate`\n- Visualize distributions with `ggdist`\n- Plot network data with `ggraph`\n\n## Packages\n\n\n::: {.cell}\n\n```{.r .cell-code}\nlibrary(ggplot2)\nlibrary(palmerpenguins) #for example dataset\nlibrary(patchwork) #for multi-panel figures\nlibrary(gridGraphics) #for combining ggplot2 and base R figures\nlibrary(gganimate) #for animated plots\n```\n:::\n\n\n# Extensions to `ggplot2`\n\nThere are many kinds of extensions to `ggplot2`, ranging from simple to complex, from familiar to transformative.\n\n## General-use and modular\n\n\n::: {.cell layout-align=\"center\"}\n\n```{.r .cell-code code-line-numbers=\"|1,4|2,5\"}\nlibrary(ggbeeswarm)\nlibrary(ggthemes)\nggplot(penguins, aes(x = species, y = body_mass_g)) +\n geom_beeswarm() +\n theme_economist()\n```\n\n::: {.cell-output-display}\n![](slides_files/figure-revealjs/unnamed-chunk-2-1.png){fig-align='center' width=960}\n:::\n:::\n\n\n::: notes\n`geom_beeswarm()` can easily be combined with other `geom_`s\n:::\n\n## \"All-in-one\" functions\n\n\n::: {.cell layout-align=\"center\"}\n\n```{.r .cell-code code-line-numbers=\"|1,2|3\"}\nlibrary(ggstatsplot)\nggbetweenstats(penguins, species, body_mass_g) +\n scale_y_continuous(\"Body Mass (g)\", n.breaks = 10)\n```\n\n::: {.cell-output-display}\n![](slides_files/figure-revealjs/unnamed-chunk-3-1.png){fig-align='center' width=960}\n:::\n:::\n\n\n::: notes\nWith great power comes great responsibility—make sure you trust these stats!\\\nNotice that the output is a ggplot object, so you can continue to add layers to it.\n:::\n\n## Transformative & field-specific\n\n::: {layout-ncol=\"2\"}\n[![ggspectra](https://exts.ggplot2.tidyverse.org/gallery/images/ggspectra.png){width=\"100%\"}](https://docs.r4photobiology.info/ggspectra)\n\n[![gggenes](https://exts.ggplot2.tidyverse.org/gallery/images/gggenes.png){width=\"100%\"}](https://wilkox.org/gggenes)\n:::\n\n## Finding Extensions\n\n- Browse the `ggplot2` [extensions gallery](https://exts.ggplot2.tidyverse.org/gallery/)\n\n- Check out the [Awesome `ggplot2`](https://github.com/erikgahner/awesome-ggplot2#readme) list\n\n- [The R Graph Gallery](https://r-graph-gallery.com/)\n\n- Google search with \"ggplot2\" keyword\n\n# Package Demos\n\n## `patchwork`\n\n`patchwork` allows you to compose multi-panel figures with ease\n\n\n::: {.cell layout-align=\"center\"}\n::: {.cell-output-display}\n![](slides_files/figure-revealjs/unnamed-chunk-4-1.png){fig-align='center' width=960}\n:::\n:::\n\n\n::: notes\nThings to notice:\n\n- Plots areas are aligned despite one not having a y-axis label\n- Only one legend\n- Labels for panels A, B, C\n:::\n\n## Example plots\n\n\n::: {.cell}\n\n```{.r .cell-code}\np1 <- \n ggplot(penguins, aes(x = body_mass_g, y = bill_length_mm, color = species)) +\n geom_point()\np2 <-\n ggplot(penguins, aes(x = bill_depth_mm, y = bill_length_mm, color = species)) +\n geom_point()\np3 <- \n ggplot(penguins, aes(x = body_mass_g)) + \n facet_wrap(vars(island)) +\n geom_histogram()\n```\n:::\n\n\n## Combine plots\n\n::: columns\n::: {.column width=\"50%\"}\n::: nonincremental\n- `+` wraps plots\n- `|` combines plots horizontally\n- `/` combines plots vertically\n- `()` can be used to nest operations\n:::\n:::\n\n::: {.column width=\"50%\"}\n\n::: {.cell}\n\n```{.r .cell-code}\n(p1 | p2) / p3\n```\n\n::: {.cell-output-display}\n![](slides_files/figure-revealjs/unnamed-chunk-6-1.png){width=480}\n:::\n:::\n\n:::\n:::\n\n## Combine guides\n\nIf plots have *identical* guides, you can combine them with `plot_layout(guides = \"collect\")`\n\n\n::: {.cell}\n\n```{.r .cell-code}\np1 + p2 + plot_layout(guides = \"collect\")\n```\n\n::: {.cell-output-display}\n![](slides_files/figure-revealjs/unnamed-chunk-7-1.png){width=960}\n:::\n:::\n\n\n::: notes\nidentical means same scale, same title, same glyph\n:::\n\n## Combining axes\n\nAs of the most recent version of `patchwork` (v1.2.0) you can also combine identical axes.\n\n\n::: {.cell}\n\n```{.r .cell-code}\np1 + p2 + plot_layout(guides = \"collect\", axes = \"collect\")\n```\n\n::: {.cell-output-display}\n![](slides_files/figure-revealjs/unnamed-chunk-8-1.png){width=960}\n:::\n:::\n\n\n## Controlling layout\n\nFor more options for controlling layout, see the related vignette on the package website.\n\n- Adding empty areas\n- Inset plots\n- Adjusting widths and heights\n\n## Tags\n\nYou can add \"tags\" to each panel with `plot_annotation()`\n\n\n::: {.cell}\n\n```{.r .cell-code}\np1 + p2 + plot_annotation(tag_levels = \"A\", tag_suffix = \")\")\n```\n\n::: {.cell-output-display}\n![](slides_files/figure-revealjs/unnamed-chunk-9-1.png){width=960}\n:::\n:::\n\n\n## Modifying all panels\n\nYou can use the `&` operator instead of `+` to modify **all** elements of a multi-panel figure.\n\n\n::: {.cell}\n\n```{.r .cell-code}\np1 + p2 & theme_bw() & scale_color_viridis_d()\n```\n\n::: {.cell-output-display}\n![](slides_files/figure-revealjs/unnamed-chunk-10-1.png){width=960}\n:::\n:::\n\n\n## Using with base R plots\n\nYou can even combine `ggplot2` plots with base R plots with a special syntax\n\n\n::: {.cell}\n\n```{.r .cell-code}\np1 + ~hist(penguins$bill_depth_mm, main = \"\")\n```\n\n::: {.cell-output-display}\n![](slides_files/figure-revealjs/unnamed-chunk-11-1.png){width=960}\n:::\n:::\n\n\n## `gganimate`\n\nDisplay transitions between states of a variable\n\nUseful for showing trends in time series & spatial data\n\n::: callout-tip\n## Advice from the experts!\n\n\"Graphic elements should only transition between instances of the same underlying phenomenon\"\n:::\n\n## Example\n\nBody size distribution of penguins changing over the years\n\n\n::: {.cell}\n\n```{.r .cell-code}\nggplot(penguins, aes(x = body_mass_g, col = species)) +\n geom_density() +\n facet_wrap(~year)\n```\n\n::: {.cell-output-display}\n![](slides_files/figure-revealjs/unnamed-chunk-12-1.png){width=960}\n:::\n:::\n\n\n## Show annual transition\n\n\n::: {.cell}\n\n```{.r .cell-code}\nlibrary(gganimate)\n\nggplot(penguins, aes(x = body_mass_g, col = species)) +\n geom_density() +\n transition_time(year) +\n ggtitle('Year: {frame_time}')\n```\n\n::: {.cell-output-display}\n![](slides_files/figure-revealjs/unnamed-chunk-13-1.gif)\n:::\n:::\n\n\n## Add history\n\n\n::: {.cell}\n\n```{.r .cell-code}\nggplot(penguins, aes(x = body_mass_g, col = species)) +\n geom_density() +\n transition_time(year) +\n ggtitle('Year: {frame_time}') +\n shadow_wake(wake = 0.3, wrap = FALSE)\n```\n\n::: {.cell-output-display}\n![](slides_files/figure-revealjs/unnamed-chunk-14-1.gif)\n:::\n:::\n\n\n## Modify transition speed\n\n\n::: {.cell}\n\n```{.r .cell-code}\nggplot(penguins, aes(x = body_mass_g, col = species)) +\n geom_density() +\n transition_time(year) +\n ggtitle('Year: {frame_time}') +\n ease_aes(\"quintic-in-out\")\n```\n\n::: {.cell-output-display}\n![](slides_files/figure-revealjs/unnamed-chunk-15-1.gif)\n:::\n:::\n\n\n## Have axes follow data\n\n\n::: {.cell}\n\n```{.r .cell-code}\nggplot(penguins, aes(x = body_mass_g, col = species)) +\n geom_density() +\n transition_time(year) +\n ggtitle('Year: {frame_time}') +\n view_follow()\n```\n\n::: {.cell-output-display}\n![](slides_files/figure-revealjs/unnamed-chunk-16-1.gif)\n:::\n:::\n\n\n## Save file\n\n\n::: {.cell}\n\n```{.r .cell-code}\nanim_save(\"~/Desktop/test_anim.gif\")\n```\n:::\n\n\n## Extensions Resources\n\n`patchwork`\n\n- [Package website](https://patchwork.data-imaginist.com/index.html)\n\n`gganimate`\n\n- [Cheat sheet](https://rstudio.github.io/cheatsheets/gganimate.pdf)\n- [Website](https://gganimate.com)\n\n## Getting Help\n\n- Our [drop-in hours](https://datascience.cct.arizona.edu/drop-in-hours)\n\n- [UA Data Science Slack](https://join.slack.com/t/uadatascience/shared_invite/zt-1xjxht9k0-E489WA6axO_SRVeKQh3cXg)\n\n# Anything we missed?\n\nGot a `ggplot2` question we didn't cover in this workshop series?\nLet's figure it out together!\n",
5+
"markdown": "---\ntitle: \"Exploring the Wide World of ggplot2 Extensions\"\nauthor: \n - \"Eric R. Scott\"\n - \"Kristina Riemer\"\n - \"Renata Diaz\"\ndate: 2024-06-20\nformat: \n uaz-revealjs: default\nexecute: \n echo: true\neditor: visual\n---\n\n\n## Learning Objectives\n\n- Understand where to find packages that extend `ggplot2`\n- Demonstrate some extension packages\n - Assemble multi-panel figures with `patchwork`\n - Make animated plots with `gganimate`\n - Visualize distributions with `ggdist`\n - Plot network data with `ggraph`\n\n## Packages\n\n\n::: {.cell}\n\n```{.r .cell-code}\nlibrary(ggplot2)\nlibrary(palmerpenguins) #for example dataset\nlibrary(patchwork) #for multi-panel figures\nlibrary(gridGraphics) #for combining ggplot2 and base R figures\nlibrary(gganimate) #for animated plots\n```\n:::\n\n\n# Extensions to `ggplot2`\n\nThere are many kinds of extensions to `ggplot2`, ranging from simple to complex, from familiar to transformative.\n\n## General-use and modular\n\n\n::: {.cell layout-align=\"center\"}\n\n```{.r .cell-code code-line-numbers=\"|1,4|2,5\"}\nlibrary(ggbeeswarm)\nlibrary(ggthemes)\nggplot(penguins, aes(x = species, y = body_mass_g)) +\n geom_beeswarm() +\n theme_economist()\n```\n\n::: {.cell-output-display}\n![](slides_files/figure-revealjs/unnamed-chunk-2-1.png){fig-align='center' width=960}\n:::\n:::\n\n\n::: notes\n`geom_beeswarm()` can easily be combined with other `geom_`s\n:::\n\n## \"All-in-one\" functions\n\n\n::: {.cell layout-align=\"center\"}\n\n```{.r .cell-code code-line-numbers=\"|1,2|3\"}\nlibrary(ggstatsplot)\nggbetweenstats(penguins, species, body_mass_g) +\n scale_y_continuous(\"Body Mass (g)\", n.breaks = 10)\n```\n\n::: {.cell-output-display}\n![](slides_files/figure-revealjs/unnamed-chunk-3-1.png){fig-align='center' width=960}\n:::\n:::\n\n\n::: notes\nWith great power comes great responsibility—make sure you trust these stats!\\\nNotice that the output is a ggplot object, so you can continue to add layers to it.\n:::\n\n## Transformative & field-specific\n\n::: {layout-ncol=\"2\"}\n[![ggspectra](https://exts.ggplot2.tidyverse.org/gallery/images/ggspectra.png){width=\"100%\"}](https://docs.r4photobiology.info/ggspectra)\n\n[![gggenes](https://exts.ggplot2.tidyverse.org/gallery/images/gggenes.png){width=\"100%\"}](https://wilkox.org/gggenes)\n:::\n\n## Finding Extensions\n\n- Browse the `ggplot2` [extensions gallery](https://exts.ggplot2.tidyverse.org/gallery/)\n\n- Check out the [Awesome `ggplot2`](https://github.com/erikgahner/awesome-ggplot2#readme) list\n\n- [The R Graph Gallery](https://r-graph-gallery.com/)\n\n- Google search with \"ggplot2\" keyword\n\n# Package Demos\n\n## `patchwork`\n\n`patchwork` allows you to compose multi-panel figures with ease\n\n\n::: {.cell layout-align=\"center\"}\n::: {.cell-output-display}\n![](slides_files/figure-revealjs/unnamed-chunk-4-1.png){fig-align='center' width=960}\n:::\n:::\n\n\n::: notes\nThings to notice:\n\n- Plots areas are aligned despite one not having a y-axis label\n- Only one legend\n- Labels for panels A, B, C\n:::\n\n## Example plots\n\n\n::: {.cell}\n\n```{.r .cell-code}\np1 <- \n ggplot(penguins, aes(x = body_mass_g, \n y = bill_length_mm, \n color = species)) +\n geom_point()\np2 <-\n ggplot(penguins, aes(x = bill_depth_mm, \n y = bill_length_mm, \n color = species)) +\n geom_point()\np3 <- \n ggplot(penguins, aes(x = body_mass_g)) + \n facet_wrap(vars(island)) +\n geom_histogram()\n```\n:::\n\n\n## Combine plots\n\n- `+` wraps plots\n- `|` combines plots horizontally\n- `/` combines plots vertically\n- `()` can be used to nest operations\n\n## Combine plots\n\n\n::: {.cell}\n\n```{.r .cell-code}\n(p1 | p2) / p3\n```\n\n::: {.cell-output-display}\n![](slides_files/figure-revealjs/unnamed-chunk-6-1.png){width=960}\n:::\n:::\n\n\n\n## Combine guides\n\nIf plots have *identical* guides, you can combine them with `plot_layout(guides = \"collect\")`\n\n\n::: {.cell}\n\n```{.r .cell-code}\np1 + p2 + plot_layout(guides = \"collect\")\n```\n\n::: {.cell-output-display}\n![](slides_files/figure-revealjs/unnamed-chunk-7-1.png){width=960}\n:::\n:::\n\n\n::: notes\nidentical means same scale, same title, same glyph\n:::\n\n## Combining axes\n\nAs of the most recent version of `patchwork` (v1.2.0) you can also combine identical axes.\n\n\n::: {.cell}\n\n```{.r .cell-code}\np1 + p2 + plot_layout(guides = \"collect\", axes = \"collect\")\n```\n\n::: {.cell-output-display}\n![](slides_files/figure-revealjs/unnamed-chunk-8-1.png){width=960}\n:::\n:::\n\n\n## Controlling layout\n\nFor more options for controlling layout, see the related vignette on the package website.\n\n- Adding empty areas\n- Inset plots\n- Adjusting widths and heights\n\n## Tags\n\nYou can add \"tags\" to each panel with `plot_annotation()`\n\n\n::: {.cell}\n\n```{.r .cell-code}\np1 + p2 + plot_annotation(tag_levels = \"A\", tag_suffix = \")\")\n```\n\n::: {.cell-output-display}\n![](slides_files/figure-revealjs/unnamed-chunk-9-1.png){width=960}\n:::\n:::\n\n\n## Modifying all panels\n\nYou can use the `&` operator instead of `+` to modify **all** elements of a multi-panel figure.\n\n\n::: {.cell}\n\n```{.r .cell-code}\np1 + p2 & theme_bw() & scale_color_viridis_d()\n```\n\n::: {.cell-output-display}\n![](slides_files/figure-revealjs/unnamed-chunk-10-1.png){width=960}\n:::\n:::\n\n\n## Using with base R plots\n\nYou can even combine `ggplot2` plots with base R plots with a special syntax\n\n\n::: {.cell}\n\n```{.r .cell-code}\np1 + ~hist(penguins$bill_depth_mm, main = \"\")\n```\n\n::: {.cell-output-display}\n![](slides_files/figure-revealjs/unnamed-chunk-11-1.png){width=960}\n:::\n:::\n\n\n## `gganimate`\n\nDisplay transitions between states of a variable\n\nUseful for showing trends in time series & spatial data\n\n::: callout-tip\n## Advice from the experts!\n\n\"Graphic elements should only transition between instances of the same underlying phenomenon\"\n:::\n\n## Example\n\nBody size distribution of penguins changing over the years\n\n\n::: {.cell}\n\n```{.r .cell-code}\nggplot(penguins, aes(x = body_mass_g, col = species)) +\n geom_density() +\n facet_wrap(~year)\n```\n\n::: {.cell-output-display}\n![](slides_files/figure-revealjs/unnamed-chunk-12-1.png){width=960}\n:::\n:::\n\n\n## Show annual transition\n\n\n::: {.cell}\n\n```{.r .cell-code}\nlibrary(gganimate)\n\nggplot(penguins, aes(x = body_mass_g, col = species)) +\n geom_density() +\n transition_time(year) +\n ggtitle('Year: {frame_time}')\n```\n\n::: {.cell-output-display}\n![](slides_files/figure-revealjs/unnamed-chunk-13-1.gif)\n:::\n:::\n\n\n## Add history\n\n\n::: {.cell}\n\n```{.r .cell-code}\nggplot(penguins, aes(x = body_mass_g, col = species)) +\n geom_density() +\n transition_time(year) +\n ggtitle('Year: {frame_time}') +\n shadow_wake(wake = 0.3, wrap = FALSE)\n```\n\n::: {.cell-output-display}\n![](slides_files/figure-revealjs/unnamed-chunk-14-1.gif)\n:::\n:::\n\n\n## Modify transition speed\n\n\n::: {.cell}\n\n```{.r .cell-code}\nggplot(penguins, aes(x = body_mass_g, col = species)) +\n geom_density() +\n transition_time(year) +\n ggtitle('Year: {frame_time}') +\n ease_aes(\"quintic-in-out\")\n```\n\n::: {.cell-output-display}\n![](slides_files/figure-revealjs/unnamed-chunk-15-1.gif)\n:::\n:::\n\n\n## Have axes follow data\n\n\n::: {.cell}\n\n```{.r .cell-code}\nggplot(penguins, aes(x = body_mass_g, col = species)) +\n geom_density() +\n transition_time(year) +\n ggtitle('Year: {frame_time}') +\n view_follow()\n```\n\n::: {.cell-output-display}\n![](slides_files/figure-revealjs/unnamed-chunk-16-1.gif)\n:::\n:::\n\n\n## Save file\n\n\n::: {.cell}\n\n```{.r .cell-code}\nanim_save(\"~/Desktop/test_anim.gif\")\n```\n:::\n\n\n## Extensions Resources\n\n`patchwork`\n\n- [Package website](https://patchwork.data-imaginist.com/index.html)\n\n`gganimate`\n\n- [Cheat sheet](https://rstudio.github.io/cheatsheets/gganimate.pdf)\n- [Website](https://gganimate.com)\n\n## Getting Help\n\n- Our [drop-in hours](https://datascience.cct.arizona.edu/drop-in-hours)\n\n- [UA Data Science Slack](https://join.slack.com/t/uadatascience/shared_invite/zt-1xjxht9k0-E489WA6axO_SRVeKQh3cXg)\n\n# Anything we missed?\n\nGot a `ggplot2` question we didn't cover in this workshop series?\nLet's figure it out together!\n",
66
"supporting": [
77
"slides_files"
88
],
Loading
Loading

0 commit comments

Comments
 (0)