-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0010_Marriage.Rmd
188 lines (110 loc) · 4 KB
/
0010_Marriage.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
---
title: "Marriage Day 10"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
```
## Hypothesis
- Detect the deviation of demographic
- Getiing Married / Demographic
- Getiing Devorced / Demographic
`glimpse(men)`
`men %>% select(-(1:3))`
```
# # change text
# both_sexesT$demographic %>% unique %>%
# gsub("Black", "黑", .) %>%
# gsub("all", "全", .) %>%
# gsub("HS", "高", .) %>%
# gsub("SC", "部大", .)
```
Get all data from current plot `ggplot_build(plot)$data`
```
plot = menNwomenL %>% filter(grepl('^(BA)', demographic)) %>% ggplot(aes(x=year, y=value, colour=sex)) + geom_line() + facet_grid(~demographic)
ggplot_build(plot)$data
```
## TODO
- starting time 08:15
> Figures represent share of the relevant population that has never been married (MARST == 6 in the IPUMS data). Note that in the story, charts generally show the share that have ever been married, which is simply 1 - n.
```{r}
men = read.table('./data/data/marriage/men.csv', header = TRUE,sep = ",")
str(men)
colnames(men)
glimpse(men)
tail(men)
```
```{r}
women = read.table('./data/data/marriage/women.csv', header = TRUE,sep = ",")
str(women)
tail(women)
```
```{r}
both_sexes = read.table('./data/data/marriage/both_sexes.csv', header = TRUE,sep = ",")
str(both_sexes)
tail(both_sexes)
```
> In the divorce file, figures are share of the relevant population that is currently divorced, conditional on having ever been married.
```{r}
divorce = read.table('./data/data/marriage/divorce.csv', header = TRUE,sep = ",")
str(divorce)
tail(divorce)
```
```{r}
menR = men
menR[-(1:3)] = 1 - menR[-(1:3)]
# menR %>% melt(id )
womenR = women
womenR[-(1:3)] = 1 - womenR[-(1:3)]
womenR
both_sexesR = both_sexes
both_sexesR[-(1:3)] = 1 - both_sexesR[-(1:3)]
both_sexesR
```
```{r}
both_sexesT = both_sexesR %>% gather(demographic, value, -c(X, year, date))
menT = menR %>% gather(demographic, value, -c(X, year, date))
womenT = womenR %>% gather(demographic, value, -c(X, year, date))
```
TODO: Maybe auto sort by decreasing order
```{r fig.width=12, fig.height=2}
# # change text
# both_sexesT$demographic %>% unique %>%
# gsub("Black", "黑", .) %>%
# gsub("all", "全", .) %>%
# gsub("HS", "高", .) %>%
# gsub("SC", "部大", .)
# change appearance order
high = c('kids_all_2534', 'kids_poor_2534', 'kids_HS_2534')
rest = setdiff(unique(both_sexesT$demographic), high)
rank= c(high, rest)
both_sexesT$demographicTmpRank = factor(both_sexesT$demographic, levels=rank)
both_sexesT %>% filter(grepl('^(kids)', demographic)) %>% ggplot(aes(x=year, y=value)) + geom_line() + facet_grid(~demographicTmpRank) + scale_x_continuous(breaks = seq(min(both_sexesT$year), max(both_sexesT$year), by = 52)) + theme(panel.spacing = unit(1, "lines"))
```
TODO: Partial Highlight
```{r}
l = both_sexesT %>% filter(grepl('^(kids_)', demographic)) %>% ggplot(aes(x=year, y=value, colour=demographic)) + geom_line() + scale_color_manual(values = c(rep('grey', 9) ) )
high = both_sexesT %>% filter(grepl('^(kids_mid_2534|kids_GD_2534)', demographic))
high
# l + geom_line(data=high, colour='red', shape=3)
```
```{r fig.width=5, fig.height=2}
menL = menT
menL$sex = factor('male')
womenL = womenT
womenL$sex = factor('female')
menNwomenL = rbind(menL, womenL)
menNwomenL %>% filter(grepl('^(all)', demographic)) %>% ggplot(aes(x=year, y=value, colour=sex)) + geom_line() + facet_grid(~demographic)
```
Very useful
```{r fig.width=7, fig.height=2}
plot = menNwomenL %>% filter(grepl('^(BA)', demographic)) %>% ggplot(aes(x=year, y=value, colour=sex)) + geom_line() + facet_grid(~demographic)
ggplot_build(plot)$data
```
TODO: good color mapping
How to do multiple line per facet?
- need to have both `male` and `female` data in the
```{r fig.width=7, fig.height=2}
menNwomenL %>% filter(grepl('^(BA)', demographic)) %>% ggplot(aes(x=year, y=value, colour=sex)) + geom_line() + facet_grid(~demographic) + scale_colour_manual(values=c(male="#00BFC4", female="#F8766D"))
```