-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03_gradient.r
106 lines (96 loc) · 2.67 KB
/
03_gradient.r
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
## ------------------------------------------------------------------------
##
## Script name: 03_gradient.r
## Purpose: Visualize educational gradients in marriage rates
## Author: Yanwen Wang
## Date Created: 2024-12-10
## Email: [email protected]
##
## ------------------------------------------------------------------------
##
## Notes:
##
## ------------------------------------------------------------------------
# 1 Load data -------------------------------------------------------------
gradient_pooled <- read_feather("Outputs/gradients.arrow")
gradient_by_hukou <- read_feather("Outputs_by_hukou/gradients.arrow")
# Recode education and urban status
gradient_pooled <- gradient_pooled %>%
mutate(
Education = case_when(
edu == 1 ~ "Primary or lower",
edu == 2 ~ "Middle school",
edu == 3 ~ "High school",
edu == 4 ~ "College or higher"
),
Education = factor(Education,
levels = c(
"Primary or lower", "Middle school",
"High school", "College or higher"
)
)
)
gradient_by_hukou <- gradient_by_hukou %>%
mutate(
Education = case_when(
edu == 1 ~ "Primary or lower",
edu == 2 ~ "Middle school",
edu == 3 ~ "High school",
edu == 4 ~ "College or higher"
),
Education = factor(Education,
levels = c(
"Primary or lower", "Middle school",
"High school", "College or higher"
)
)
) %>%
mutate(urban = ifelse(urban == 1, "Rural", "Urban"))
# 2 Plot trends -----------------------------------------------------------
# Pooled
gradient_pooled_plt <- ggplot(
gradient_pooled,
aes(x = birthy, y = ratio, color = Education, group = Education)
) +
geom_point(alpha = 0.75) +
geom_smooth(span = 0.8, se = FALSE) +
scale_color_manual(
values = c(
"#fd7f6f", "#7eb0d5", "#b2e061", "#bd7ebe",
"#ffb55a", "#ffee65", "#beb9db", "#fdcce5", "#8bd3c7"
)
) +
labs(
x = "Cohort",
y = "Ratio of unmarried to married"
) +
theme(legend.position = "none") +
facet_grid(~Gender)
# By hukou
gradient_by_hukou_plt <- ggplot(
gradient_by_hukou,
aes(x = birthy, y = ratio, color = Education, group = Education)
) +
geom_point(alpha = 0.75) +
geom_smooth(span = 0.8, se = FALSE) +
scale_color_manual(
values = c(
"#fd7f6f", "#7eb0d5", "#b2e061", "#bd7ebe",
"#ffb55a", "#ffee65", "#beb9db", "#fdcce5", "#8bd3c7"
)
) +
labs(
x = "Cohort",
y = "Ratio of unmarried to married"
) +
theme(legend.position = "bottom") +
facet_grid(~urban+Gender)
gradient_plt <- gradient_pooled_plt / gradient_by_hukou_plt
# Save the plot
ggsave(
"Graphs/gradient.png",
gradient_plt,
width = 8,
height = 12,
dpi = 300
)