Skip to content

Commit

Permalink
Merge pull request #23 from cct-datascience/gganim
Browse files Browse the repository at this point in the history
Add gganimate slides
  • Loading branch information
KristinaRiemer authored Jun 20, 2024
2 parents d66416e + 1581851 commit c14c239
Show file tree
Hide file tree
Showing 13 changed files with 116 additions and 21 deletions.
112 changes: 93 additions & 19 deletions 2024/03-extensions/slides.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ editor: visual
## Learning Objectives

- Understand where to find packages that extend `ggplot2`
- Assemble multi-panel figures with `patchwork`
- Make animated plots with `gganimate`
- Visualize distributions with `ggdist`
- Plot network data with `ggraph`
- Demonstrate some extension packages
- Assemble multi-panel figures with `patchwork`
- Make animated plots with `gganimate`
- Visualize distributions with `ggdist`
- Plot network data with `ggraph`

## Packages

Expand Down Expand Up @@ -119,10 +120,14 @@ Things to notice:

```{r}
p1 <-
ggplot(penguins, aes(x = body_mass_g, y = bill_length_mm, color = species)) +
ggplot(penguins, aes(x = body_mass_g,
y = bill_length_mm,
color = species)) +
geom_point()
p2 <-
ggplot(penguins, aes(x = bill_depth_mm, y = bill_length_mm, color = species)) +
ggplot(penguins, aes(x = bill_depth_mm,
y = bill_length_mm,
color = species)) +
geom_point()
p3 <-
ggplot(penguins, aes(x = body_mass_g)) +
Expand All @@ -132,24 +137,17 @@ p3 <-

## Combine plots

::: columns
::: {.column width="50%"}
::: nonincremental
- `+` wraps plots
- `|` combines plots horizontally
- `/` combines plots vertically
- `()` can be used to nest operations
:::
:::

::: {.column width="50%"}
## Combine plots

```{r}
#| fig-width: 5
#| fig-height: 5
(p1 | p2) / p3
```
:::
:::


## Combine guides

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

## Getting Help
## `gganimate`

Display transitions between states of a variable

Useful for showing trends in time series & spatial data

::: callout-tip
## Advice from the experts!

- `patchwork`: [Package website](https://patchwork.data-imaginist.com/index.html)
"Graphic elements should only transition between instances of the same underlying phenomenon"
:::

## Example

Body size distribution of penguins changing over the years

```{r}
ggplot(penguins, aes(x = body_mass_g, col = species)) +
geom_density() +
facet_wrap(~year)
```

## Show annual transition

```{r}
library(gganimate)
ggplot(penguins, aes(x = body_mass_g, col = species)) +
geom_density() +
transition_time(year) +
ggtitle('Year: {frame_time}')
```

- `gganimate`: [Getting started guide](https://gganimate.com/articles/gganimate.html), [reference](https://gganimate.com/reference/index.html)
## Add history

```{r}
ggplot(penguins, aes(x = body_mass_g, col = species)) +
geom_density() +
transition_time(year) +
ggtitle('Year: {frame_time}') +
shadow_wake(wake = 0.3, wrap = FALSE)
```

## Modify transition speed

```{r}
ggplot(penguins, aes(x = body_mass_g, col = species)) +
geom_density() +
transition_time(year) +
ggtitle('Year: {frame_time}') +
ease_aes("quintic-in-out")
```

## Have axes follow data

```{r}
ggplot(penguins, aes(x = body_mass_g, col = species)) +
geom_density() +
transition_time(year) +
ggtitle('Year: {frame_time}') +
view_follow()
```

## Save file

```{r, eval=FALSE}
anim_save("~/Desktop/test_anim.gif")
```

## Extensions Resources

`patchwork`

- [Package website](https://patchwork.data-imaginist.com/index.html)

`gganimate`

- [Cheat sheet](https://rstudio.github.io/cheatsheets/gganimate.pdf)
- [Website](https://gganimate.com)

## Getting Help

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"hash": "c113ce04074280222242a4df9cb19c6c",
"result": {
"engine": "knitr",
"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",
"supporting": [
"slides-gganimate_files"
],
"filters": [
"rmarkdown/pagebreak.lua"
],
"includes": {
"include-after-body": [
"\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"
]
},
"engineDependencies": {},
"preserve": {},
"postProcess": true
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit c14c239

Please sign in to comment.