forked from perlatex/R_for_Data_Science
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eda_migration.Rmd
227 lines (172 loc) · 4.7 KB
/
eda_migration.Rmd
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# 探索性数据分析-移民缺口 {#eda-migration}
## 引言
今天看到一张图,觉得很不错,简单清晰。
```{r, out.width = '90%', echo = FALSE}
knitr::include_graphics(path = "images/migration.png")
```
数据是公开的,因此不难找到,我是在[这里](https://www.cato.org/blog/where-did-24-million-48-million-missing-immigrants-go)图中获取。
**先观察这张图想表达的意思:**
- 蓝色的是历年移民人口真实数据
- 依据前6个点(2011年到2016年)建立线性模型,并依此预测后5个点(2016到2021年)的情况,从而得到黄色的直线
- 预测情况与实际情况的差,得到缺口总数210万
## 开始
```{r, message=FALSE, warning=FALSE}
library(tidyverse)
library(modelr)
df <- tibble::tribble(
~year, ~num,
2011, 795300,
2012, 858740,
2013, 849730,
2014, 945640,
2015, 1060000,
2016, 1065000,
2017, 948390,
2018, 719870,
2019, 568540,
2020, 477030
) %>%
mutate(num = num / 1000000)
df
```
### 线性回归模型
依据2011到2016年的数据,建立线性模型
```{r, message=FALSE, warning=FALSE}
mod <- df %>%
filter(year <= 2016) %>%
lm(num ~ 1 + year, data = .)
```
### 预测
根据线性模型预测2016年到2020的情况
```{r, message=FALSE, warning=FALSE}
pred_df <- tibble(
year = seq(2016, 2020, by = 1)
) %>%
modelr::add_predictions(model = mod)
pred_df
```
合并成新的数据框
```{r}
combine_df <- df %>%
left_join(pred_df, by = "year")
combine_df
```
```{r, eval=FALSE}
# 一个等价的方法
df %>%
modelr::add_predictions(model = mod) %>%
mutate(pred = if_else(year < 2016, NA_real_, pred))
```
### 可视化
- 基本绘图,画折线图和散点图
```{r}
combine_df %>%
ggplot(aes(x = year, y = num)) +
geom_point() +
geom_line() +
geom_line(aes(y = pred)) +
geom_point(aes(y = pred))
```
- 调整坐标和配色
```{r}
mycolor <- c("#3D81E0", "#f4a261")
scales::show_col(mycolor)
```
```{r}
combine_df %>%
ggplot(aes(x = year, y = num)) +
geom_point(size = 4, fill = "#3D81E0", color = "#3D81E0", shape = 23) +
geom_line(size = 2, color = "#3D81E0") +
geom_line(aes(y = pred), size = 2, color = "#f4a261") +
geom_point(aes(y = pred), size = 4, fill = "#f4a261", color = "#f4a261", shape = 23) +
labs(
title = "Figure2: Contribution of international migration to population growth",
subtitle = "millions per year",
caption = "Source: Census Bureau, J.P. Morgan",
x = NULL,
y = NULL
) +
scale_y_continuous(
limits = c(0, 1.5),
breaks = seq(0, 1.5, by = 0.25),
expand = c(0, 0)
) +
scale_x_continuous(
limits = c(2011, 2021),
breaks = seq(2011, 2021, by = 1),
expand = c(0.1, 0)
) +
theme_classic(base_size = 14) +
theme(
legend.position = "none",
plot.title.position = 'plot',
plot.caption = element_text(hjust = 0)
)
```
- 添加标注
```{r}
arrows <- tibble::tribble(
~x1, ~y1, ~x2, ~y2, ~color,
2016, 0.54, 2017.5, 0.75, "a",
2017, 1.42, 2018.0, 1.25, "b"
)
combine_df %>%
ggplot(aes(x = year, y = num)) +
geom_point(size = 4, fill = "#3D81E0", color = "#3D81E0", shape = 23) +
geom_line(size = 2, color = "#3D81E0") +
geom_line(aes(y = pred), size = 2, color = "#f4a261") +
geom_point(aes(y = pred), size = 4, fill = "#f4a261", color = "#f4a261", shape = 23) +
geom_ribbon(
aes(ymin = num, ymax = pred),
fill = "orange",
alpha = 0.2
) +
geom_segment(
data = arrows,
aes(x = x1, y = y1, xend = x2, yend = y2, color = color),
arrow = arrow(length = unit(0.15, "inch")), size = 1.5
) +
annotate("text",
x = c(2017, 2016, 2021), y = c(1.47, 0.5, 0.9),
size = 6, face = "bold",
label = c("Pre-2017 trend", "Actual", "Shortfall:\n 2.1 million")
) +
labs(
title = "Figure2: Contribution of international migration to population growth",
subtitle = "millions per year",
caption = "Source: Census Bureau, J.P. Morgan",
x = NULL,
y = NULL
) +
scale_y_continuous(
limits = c(0, 1.5),
breaks = seq(0, 1.5, by = 0.25),
expand = c(0, 0)
) +
scale_x_continuous(
limits = c(2011, 2021),
breaks = seq(2011, 2021, by = 1),
expand = c(0.1, 0)
) +
scale_color_manual(
values = c(a = "#3D81E0", b = "#f4a261")
) +
theme_classic(base_size = 14) +
theme(
legend.position = "none",
plot.title.position = 'plot',
plot.caption = element_text(hjust = 0)
)
```
- 保存
```{r, eval = FALSE}
ggsave("migration.pdf", width = 8, height = 5)
```
```{r, echo = F}
# remove the objects
# rm(list=ls())
rm(arrows, combine_df, mycolor, df, pred_df, mod)
```
```{r, echo = F, message = F, warning = F, results = "hide"}
pacman::p_unload(pacman::p_loaded(), character.only = TRUE)
```