Skip to content

Instantly share code, notes, and snippets.

@vpnagraj
Created October 8, 2024 02:45
Show Gist options
  • Save vpnagraj/bfc89038040967388309a38e296b8130 to your computer and use it in GitHub Desktop.
Save vpnagraj/bfc89038040967388309a38e296b8130 to your computer and use it in GitHub Desktop.
Examples of animating data visualizations in R
---
title: "Animated Data Visualizations in R"
execute:
warning: false
message: false
---
## Load packages
First, we load the packages that we'll use below:
```{r}
library(tidyverse)
#remotes::install_github("hrbrmstr/cdcfluview")
library(cdcfluview)
library(MMWRweek)
library(tidyverse)
library(plotly)
library(gganimate)
```
## Retrieve and prep data
The code below will pull data from ILInet and then do some minimal processing to filter for Virginia and Texas:
```{r}
## pull data from ILInet via cdcfluview package
ili <- ilinet(region = "state")
## data prep for virginia and texas
vatx <-
ili %>%
## convert date from epiyear and epiweek
mutate(date = MMWRweek2Date(year, week)) %>%
## filter for a date range
filter(date >= as.Date("2016-09-01") & date <= as.Date("2019-09-01")) %>%
## format year week combined
mutate(yweek = paste0(year, "-", week)) %>%
## restrict to just virginia and texas
filter(region %in% c("Virginia","Texas"))
```
## Static plot
Next we can create a a static plot using some basic `ggplot2`:
```{r}
p <-
## create base layer for date on x axis and ili on y
ggplot(vatx, aes(date, unweighted_ili)) +
## add line with color by state and frame for the year
geom_line(aes(frame = year, group = region, color = region)) +
## set to minimal theme
theme_minimal() +
## put the legend on the bottom
theme(legend.position = "bottom")
p
```
## `plotly`
The `ggplot2` object can be directly converted with `ggplotly()` and customized with animation options:
```{r}
fig <-
## convert static ggplot2 plot to plotly object
ggplotly(p) %>%
## set animation options
animation_opts(frame = 2000, easing = "linear", redraw = FALSE
) %>%
## define where to place the "play" button for animation
animation_button(
x = 1, xanchor = "right", y = -0.75, yanchor = "bottom"
) %>%
## add text to the slider
animation_slider(
currentvalue = list(prefix = "YEAR ", font = list(color="black"))
)
fig
```
## `gganimate`
The `gganimate` package can also animate a `ggplot2` object with `transition_time()`:
```{r}
## add transition with transition_time
p +
transition_time(year)
```
And also with `transition_reveal()`:
```{r}
## add transition with transition_reveal
p +
transition_reveal(date)
```
## -------------------------------------------------------------------------------------------------------------------
library(tidyverse)
#remotes::install_github("hrbrmstr/cdcfluview")
library(cdcfluview)
library(MMWRweek)
library(tidyverse)
library(plotly)
library(gganimate)
## -------------------------------------------------------------------------------------------------------------------
## pull data from ILInet via cdcfluview package
ili <- ilinet(region = "state")
## data prep for virginia and texas
vatx <-
ili %>%
## convert date from epiyear and epiweek
mutate(date = MMWRweek2Date(year, week)) %>%
## filter for a date range
filter(date >= as.Date("2016-09-01") & date <= as.Date("2019-09-01")) %>%
## format year week combined
mutate(yweek = paste0(year, "-", week)) %>%
## restrict to just virginia and texas
filter(region %in% c("Virginia","Texas"))
## -------------------------------------------------------------------------------------------------------------------
p <-
## create base layer for date on x axis and ili on y
ggplot(vatx, aes(date, unweighted_ili)) +
## add line with color by state and frame for the year
geom_line(aes(frame = year, group = region, color = region)) +
## set to minimal theme
theme_minimal() +
## put the legend on the bottom
theme(legend.position = "bottom")
p
## -------------------------------------------------------------------------------------------------------------------
fig <-
## convert static ggplot2 plot to plotly object
ggplotly(p) %>%
## set animation options
animation_opts(frame = 2000, easing = "linear", redraw = FALSE
) %>%
## define where to place the "play" button for animation
animation_button(
x = 1, xanchor = "right", y = -0.75, yanchor = "bottom"
) %>%
## add text to the slider
animation_slider(
currentvalue = list(prefix = "YEAR ", font = list(color="black"))
)
fig
## -------------------------------------------------------------------------------------------------------------------
## add transition with transition_time
p +
transition_time(year)
## -------------------------------------------------------------------------------------------------------------------
## add transition with transition_reveal
p +
transition_reveal(date)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment