@@ -5,64 +5,79 @@ title: gganimate
5
5
language : R
6
6
---
7
7
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
-
12
8
### Installation
13
9
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" ))
20
13
```
21
14
22
15
### Example
23
16
24
17
* Example using weather data
25
18
* Get this data using ` rnoaa `
26
19
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" )
27
27
```
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
+
32
29
* Plot annual temperature pattern through time
33
30
* First make a plot of the annual temperature trend using ggplot
34
31
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()
35
39
```
36
- library(ggplot2)
37
- library(gganimate)
38
40
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
41
45
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 )
44
50
```
45
51
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
47
54
55
+ ``` r
56
+ ggplot(gnv_weather , aes(x = jday , y = tmax )) +
57
+ geom_line() +
58
+ labs(title = ' Year: {frame_time}' ) +
59
+ transition_time(year )
48
60
```
49
- p <- ggplot(gnv_weather, aes(x = jday, y = tmax / 10, frame = year)) +
50
- geom_point()
51
61
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 )
53
70
```
54
71
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() `
57
73
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 )
68
83
```
0 commit comments