Skip to content

Commit 1b45129

Browse files
committed
Update gganimate to new API and add complexity
1 parent 7739fb8 commit 1b45129

File tree

1 file changed

+51
-36
lines changed

1 file changed

+51
-36
lines changed

materials/gganimate.md

+51-36
Original file line numberDiff line numberDiff line change
@@ -5,64 +5,79 @@ title: gganimate
55
language: R
66
---
77

8-
> * ImageMagick needs to be installed to save as gif (the default)
9-
> * Save to html file if installing ImageMagick isn't an option
10-
> * E.g., gganimate(p, "output.html")
11-
128
### Installation
139

14-
* Some R packages are not published to CRAN
15-
* Can install from other places using `devtools`
16-
17-
```
18-
install.packages("devtools")
19-
devtools::install_github("dgrtwo/gganimate")
10+
```r
11+
# gifski required for default gif output
12+
install.packages(c("gganimate", "gifski", "rnoaa"))
2013
```
2114

2215
### Example
2316

2417
* Example using weather data
2518
* Get this data using `rnoaa`
2619

20+
```r
21+
library(gganimate)
22+
library(ggplot2)
23+
library(lubridate)
24+
library(rnoaa)
25+
26+
gnv_weather <- meteo_pull_monitors("USW00012816", date_min = "1984-01-01", date_max = "2024-12-31")
2727
```
28-
install.packages('rnoaa')
29-
library('rnoaa')
30-
gnv_weather <- meteo_pull_monitors("USW00012816", date_min = "1984-01-01", date_max = "2016-12-31")
31-
```
28+
3229
* Plot annual temperature pattern through time
3330
* First make a plot of the annual temperature trend using ggplot
3431

32+
```r
33+
gnv_weather$year = as.integer(year(gnv_weather$date))
34+
gnv_weather$jday = yday(gnv_weather$date)
35+
gnv_weather$tmax = gnv_weather$tmax / 10
36+
37+
ggplot(gnv_weather, aes(x = jday, y = tmax)) +
38+
geom_line()
3539
```
36-
library(ggplot2)
37-
library(gganimate)
3840

39-
gnv_weather$year = format(gnv_weather$date, "%Y")
40-
gnv_weather$jday = as.numeric(format(gnv_weather$date, "%j"))
41+
* This plot shows the temperatures of each day of the year for all years from 1984 to 2024
42+
* Let's animate them so we can see the pattern over time
43+
44+
* Add `transition_time` element to
4145

42-
ggplot(gnv_weather, aes(x = jday, y = tmax / 10)) +
43-
geom_point()
46+
```r
47+
ggplot(gnv_weather, aes(x = jday, y = tmax)) +
48+
geom_line() +
49+
transition_time(year)
4450
```
4551

46-
* Then add `frame` element to `aes()`
52+
* Now let's add some information on year to the graph
53+
* Do this by adding a title that shows the time (in our case year) for each frame
4754

55+
```r
56+
ggplot(gnv_weather, aes(x = jday, y = tmax)) +
57+
geom_line() +
58+
labs(title = 'Year: {frame_time}') +
59+
transition_time(year)
4860
```
49-
p <- ggplot(gnv_weather, aes(x = jday, y = tmax / 10, frame = year)) +
50-
geom_point()
5161

52-
gganimate(p)
62+
* Add a shadow mark to the plot so that we can see where previous years were
63+
64+
```r
65+
ggplot(gnv_weather, aes(x = jday, y = tmax)) +
66+
geom_line() +
67+
labs(title = 'Year: {frame_time}') +
68+
transition_time(year) +
69+
shadow_mark(color = 'gray', linewidth = 0.2)
5370
```
5471

55-
* Uses the `animation` package
56-
* More complicated but more powerful
72+
* If we want more control over the animation store the ggplot object and use `animate()`
5773

58-
```
59-
install.packages('animation')
60-
library(animation)
61-
62-
saveGIF({
63-
for (yr in 1984:2016){
64-
data = dplyr::filter(gnv_weather, year == yr)
65-
plot(data$jday, data$tmax)
66-
}
67-
})
74+
```r
75+
p <- ggplot(gnv_weather, aes(x = jday, y = tmax)) +
76+
geom_line() +
77+
labs(title = 'Year: {frame_time}') +
78+
transition_time(year) +
79+
shadow_mark(color = 'gray', linewidth = 0.2) +
80+
ease_aes('linear')
81+
82+
animate(p, fps = 2, end_pause = 50)
6883
```

0 commit comments

Comments
 (0)