-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathV43_gganimate.Rmd
More file actions
108 lines (85 loc) · 3.58 KB
/
V43_gganimate.Rmd
File metadata and controls
108 lines (85 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
```{r include=FALSE}
knitr::opts_chunk$set(cache = T, warning = F, message = F,
class.output = "output", out.width='100%',
fig.asp = 0.5, fig.align = 'center')
```
## 產製圖表動畫
https://gist.github.com/rafapereirabr/0d68f7ccfc3af1680c4c8353cf9ab345
```{r include=FALSE}
library(tidyverse)
library(readxl)
options(scipen = 999)
```
R也有套工具可以產製圖表動畫,概念上就是沿著一條資料維度,把多張圖給疊在一起變成一個gif動畫。本例子即是把產假之薪的範例沿著時間軸做動畫。每個時間點都是當年各國產假支薪給付程度的地圖,但由於有19年的資料,所以可以把年代當成動畫的時間軸。
以下是清理資料的步驟,會彙整出國名、國家代碼(ISO3)、年、和給付等級四個變項。預期利用國名、國家代碼和給付等級就可以畫出每年的圖。然後將年作為動畫的時間軸,便可產生地圖動畫。
```{r ani-prepare-data}
pml <- read_excel("data/WORLD-MACHE_Gender_6.8.15.xls", "Sheet1", col_names=T) %>%
select(country, iso3, contains("matleave"), -contains("wrr")) %>%
gather("year", "degree", 3:21) %>%
replace_na(list(degree=0)) %>%
mutate(year2=as.POSIXct(strptime(year, "matleave_%y"))) %>%
mutate(year3 = strftime(year2, "%Y")) %>%
select(country, ISO3=iso3, year=year3, degree)
```
### 地圖下載與轉換投影方法
此為下載地圖並處理地圖成為可以用`geom_polygom()`繪圖的多邊形資料點。
```{r ani-get-worldmap}
library(rworldmap)
wmap <- getMap(resolution="low")
wmap <- spTransform(wmap, CRS("+proj=robin")) # reproject
wmap <- fortify(wmap)
wmap %>%
filter(!duplicated(id)) %>% head(10)
```
```{r ani-join-map-data}
pml_map <- wmap %>%
left_join(pml, by=c("id"="country")) %>%
filter(!is.na(ISO3)) %>%
mutate(year = as.integer(year))
# devtools::install_github("thomasp85/transformr")
pml_map %>%
select(id) %>%
filter(!duplicated(.)) %>% head(10)
```
### 靜態繪圖測試
```{r ani-plot-testing}
pml_map %>%
filter(year==1995) %>%
ggplot() +
aes(x = long, y = lat,
group=group, fill=factor(degree)) +
geom_polygon(color="grey") +
theme_void() +
scale_fill_manual(values=c("1"="red",
"2"="LightCyan",
"3"="lightskyblue",
"4"="DodgerBlue",
"5"="MediumBlue")) +
coord_cartesian(xlim = c(-11807982, 14807978))
```
在採用gganimate繪圖時,僅需要多加一個動畫繪圖函式`+ transition_time(year)`即可,其他繪圖部分並無修改。最後才用`animate()`函式把這整個繪圖指令轉製為動畫,包含指定`fps`(frame per second)和長寬等參數。
```{r ani-animating}
library(gganimate)
pml.ani <- pml_map %>%
ggplot() +
aes(x = long, y = lat,
group=group, fill=factor(degree)) +
geom_polygon(color="grey") +
theme_void() +
scale_fill_manual(values=c("1"="red",
"2"="LightCyan",
"3"="lightskyblue",
"4"="DodgerBlue",
"5"="MediumBlue")) +
coord_cartesian(xlim = c(-11807982, 14807978)) +
transition_time(year)
# +
# ease_aes("linear") +
# enter_fade() +
# exit_fade()
animate(pml.ani, fps = 10, end_pause = 30, width = 750, height = 450, renderer = gifski_renderer())
anim_save("jour5014/pml2.gif", animation = last_animation())
```
```{r}
knitr::include_graphics("jour5014/pml2.gif")
```