Skip to content

Commit c14c239

Browse files
Merge pull request #23 from cct-datascience/gganim
Add gganimate slides
2 parents d66416e + 1581851 commit c14c239

File tree

13 files changed

+116
-21
lines changed

13 files changed

+116
-21
lines changed

2024/03-extensions/slides.qmd

Lines changed: 93 additions & 19 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

@@ -203,11 +201,87 @@ You can even combine `ggplot2` plots with base R plots with a special syntax
203201
p1 + ~hist(penguins$bill_depth_mm, main = "")
204202
```
205203

206-
## Getting Help
204+
## `gganimate`
205+
206+
Display transitions between states of a variable
207+
208+
Useful for showing trends in time series & spatial data
209+
210+
::: callout-tip
211+
## Advice from the experts!
207212

208-
- `patchwork`: [Package website](https://patchwork.data-imaginist.com/index.html)
213+
"Graphic elements should only transition between instances of the same underlying phenomenon"
214+
:::
215+
216+
## Example
217+
218+
Body size distribution of penguins changing over the years
219+
220+
```{r}
221+
ggplot(penguins, aes(x = body_mass_g, col = species)) +
222+
geom_density() +
223+
facet_wrap(~year)
224+
```
225+
226+
## Show annual transition
227+
228+
```{r}
229+
library(gganimate)
230+
231+
ggplot(penguins, aes(x = body_mass_g, col = species)) +
232+
geom_density() +
233+
transition_time(year) +
234+
ggtitle('Year: {frame_time}')
235+
```
209236

210-
- `gganimate`: [Getting started guide](https://gganimate.com/articles/gganimate.html), [reference](https://gganimate.com/reference/index.html)
237+
## Add history
238+
239+
```{r}
240+
ggplot(penguins, aes(x = body_mass_g, col = species)) +
241+
geom_density() +
242+
transition_time(year) +
243+
ggtitle('Year: {frame_time}') +
244+
shadow_wake(wake = 0.3, wrap = FALSE)
245+
```
246+
247+
## Modify transition speed
248+
249+
```{r}
250+
ggplot(penguins, aes(x = body_mass_g, col = species)) +
251+
geom_density() +
252+
transition_time(year) +
253+
ggtitle('Year: {frame_time}') +
254+
ease_aes("quintic-in-out")
255+
```
256+
257+
## Have axes follow data
258+
259+
```{r}
260+
ggplot(penguins, aes(x = body_mass_g, col = species)) +
261+
geom_density() +
262+
transition_time(year) +
263+
ggtitle('Year: {frame_time}') +
264+
view_follow()
265+
```
266+
267+
## Save file
268+
269+
```{r, eval=FALSE}
270+
anim_save("~/Desktop/test_anim.gif")
271+
```
272+
273+
## Extensions Resources
274+
275+
`patchwork`
276+
277+
- [Package website](https://patchwork.data-imaginist.com/index.html)
278+
279+
`gganimate`
280+
281+
- [Cheat sheet](https://rstudio.github.io/cheatsheets/gganimate.pdf)
282+
- [Website](https://gganimate.com)
283+
284+
## Getting Help
211285

212286
- Our [drop-in hours](https://datascience.cct.arizona.edu/drop-in-hours)
213287

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"hash": "c113ce04074280222242a4df9cb19c6c",
3+
"result": {
4+
"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::: {.cell}\n\n```{.r .cell-code}\nlibrary(ggplot2)\nlibrary(palmerpenguins)\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-gganimate_files/figure-revealjs/unnamed-chunk-2-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_states(year) +\n ggtitle('Year: {closest_state}')\n```\n\n::: {.cell-output-display}\n![](slides-gganimate_files/figure-revealjs/unnamed-chunk-3-1.gif)\n:::\n:::\n\n\n## Add more detail\n\n\n::: {.cell}\n\n```{.r .cell-code}\nggplot(penguins, aes(x = body_mass_g, col = species)) +\n geom_density() +\n transition_states(year) +\n ggtitle('Year: {closest_state}') +\n shadow_wake(wake = 0.25)\n```\n\n::: {.cell-output-display}\n![](slides-gganimate_files/figure-revealjs/unnamed-chunk-4-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## Resources\n\n- Cheat sheet: <https://rstudio.github.io/cheatsheets/gganimate.pdf>\n- Website: <https://gganimate.com>\n",
6+
"supporting": [
7+
"slides-gganimate_files"
8+
],
9+
"filters": [
10+
"rmarkdown/pagebreak.lua"
11+
],
12+
"includes": {
13+
"include-after-body": [
14+
"\n<script>\n // htmlwidgets need to know to resize themselves when slides are shown/hidden.\n // Fire the \"slideenter\" event (handled by htmlwidgets.js) when the current\n // slide changes (different for each slide format).\n (function () {\n // dispatch for htmlwidgets\n function fireSlideEnter() {\n const event = window.document.createEvent(\"Event\");\n event.initEvent(\"slideenter\", true, true);\n window.document.dispatchEvent(event);\n }\n\n function fireSlideChanged(previousSlide, currentSlide) {\n fireSlideEnter();\n\n // dispatch for shiny\n if (window.jQuery) {\n if (previousSlide) {\n window.jQuery(previousSlide).trigger(\"hidden\");\n }\n if (currentSlide) {\n window.jQuery(currentSlide).trigger(\"shown\");\n }\n }\n }\n\n // hookup for slidy\n if (window.w3c_slidy) {\n window.w3c_slidy.add_observer(function (slide_num) {\n // slide_num starts at position 1\n fireSlideChanged(null, w3c_slidy.slides[slide_num - 1]);\n });\n }\n\n })();\n</script>\n\n"
15+
]
16+
},
17+
"engineDependencies": {},
18+
"preserve": {},
19+
"postProcess": true
20+
}
21+
}
Loading
Loading
Loading

0 commit comments

Comments
 (0)