-
Notifications
You must be signed in to change notification settings - Fork 0
/
weight_plot.Rmd
335 lines (287 loc) · 13.3 KB
/
weight_plot.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
2002~2008년과 2009~2015년의 검진 방식 간 차이가 있어 먼저 09년만 살펴봤습니다
drink는 월별 평균 알코올 양으로 0~60 값을 가지며 평균 10.765입니다
workout은 월별 평균 운동량으로 0~6 값을 가지며 평균 1.393입니다.
```{r include=FALSE}
#패키지 및 데이터 불러오기
lapply(c('dplyr',
'data.table',
'reshape',
'haven',
'rowr',
'lubridate',
'ggplot2',
'gridExtra'), require, character.only=T)
setwd('C:\\Users\\sseo\\Desktop\\R\\weight_plot')
lf <- grep('dat', list.files(), value=T)
l1 <- lapply(1:4, function(x) read.csv(lf[x]) %>%
select(-1) %>%
mutate(day = as.factor(day))) %>%
rbindlist(idcol = T) %>%
mutate(sex = ifelse(.id == 1 | .id == 3, 'men', 'women') %>% as.factor(),
year = ifelse(.id == 1 | .id == 2, 'former', 'latter') %>% as.factor()) %>%
select(-.id) %>% select(sex, year, 1:7)
```
##09년 이후 / 여자 / 키 & 체중 & BMI
```{r echo=F, fig.height = 10, fig.width = 25}
wom09 <- l1 %>%
filter(year == 'latter', sex == 'women') %>%
mutate(month = substr(as.character(day), 1, 6)) %>%
group_by(month) %>%
summarise(weight = mean(weight),
height = mean(height),
BMI = mean(BMI),
drink = mean(drink),
workout = mean(workout)) %>%
mutate(month = paste0(month, '01'))
ggplot(wom09, aes(x = as.factor(month))) +
geom_line(aes(y = scale(height) , group = 1, colour = 'height'), size=1.5) +
geom_line(aes(y = scale(weight) -4, group = 2, colour = 'weight'), size=1.5) +
geom_line(aes(y = scale(BMI) + 2.5, group = 3, colour = 'BMI'), size=1.5) +
theme_minimal() +
scale_x_discrete(breaks = paste0(2009:2015, '0101')) +
theme(axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.x = element_text(size = rel(2)),
axis.text.y = element_text(size = rel(2)),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
legend.position = c(1,0),
legend.justification = c(1,0),
legend.background = element_rect(size=rel(2)),
legend.title = element_blank(),
legend.text = element_text(size=rel(4)),
legend.key.size = unit(1, 'cm')) +
geom_vline(xintercept = which(wom09$month %in% paste0(rep(2009:2015, 4), c('0301', '0601', '0901', '1201'))),
linetype='dashed', alpha=.2, size=.05) +
geom_vline(xintercept = which(wom09$month %in% paste0(rep(2009:2015), '0101'))) +
ggtitle('변수의 패턴 간 연관성을 보기위해 scaling')
```
```{r echo = F}
#3차원도 봐야
#library(plotly)
```
##09년 이후 / 여자 / 체중 & 음주량
```{r echo=F, fig.height = 7, fig.width = 18}
ggplot(wom09,
aes(x=as.factor(month), y=scale(weight) + 2, group=1, colour='weight')) +
geom_line(size=1.5) +
geom_line(aes(y=scale(drink), colour='drink'), size=1.5) +
theme_minimal() +
scale_y_continuous(sec.axis = sec_axis(~., name = 'workout')) +
scale_x_discrete(breaks = paste0(2009:2015, '0101')) +
theme(axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.x = element_text(size = rel(2)),
axis.text.y = element_text(size = rel(2)),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
legend.position = c(1,0),
legend.justification = c(1,0),
legend.background = element_rect(size=rel(2)),
legend.title = element_blank(),
legend.text = element_text(size=rel(2))) +
geom_vline(xintercept = which(wom09$month %in% paste0(rep(2009:2015, 4), c('0301', '0601', '0901', '1201'))),
linetype='dashed', alpha=.2, size=.05) +
geom_vline(xintercept = which(wom09$month %in% paste0(rep(2009:2015), '0101')))
```
##09년 이후 / 여자 / 체중 & 운동량
```{r echo=F, fig.height = 7, fig.width = 18}
ggplot(wom09,
aes(x=as.factor(month), y=scale(weight) + 2, group=1, colour='weight')) +
geom_line(size=1.5) +
geom_line(aes(y=scale(workout), colour='workout'), size=1.5) +
theme_minimal() +
scale_y_continuous(sec.axis = sec_axis(~., name = 'workout')) +
scale_x_discrete(breaks = paste0(2009:2015, '0101')) +
theme(axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.x = element_text(size = rel(2)),
axis.text.y = element_text(size = rel(2)),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
legend.position = c(1,0),
legend.justification = c(1,0),
legend.background = element_rect(size=rel(2)),
legend.title = element_blank(),
legend.text = element_text(size=rel(2))) +
geom_vline(xintercept = which(wom09$month %in% paste0(rep(2009:2015, 4), c('0301', '0601', '0901', '1201'))),
linetype='dashed', alpha=.2, size=.05) +
geom_vline(xintercept = which(wom09$month %in% paste0(rep(2009:2015), '0101')))
```
##09년 이후 / 여자 / BMI & 음주량
```{r echo=F, fig.height = 7, fig.width = 18}
ggplot(wom09,
aes(x=as.factor(month), y=scale(BMI)+2, group=1, colour='BMI')) +
geom_line(size=1.5) +
geom_line(aes(y=scale(drink), colour='drink'), size=1.5) +
theme_minimal() +
scale_y_continuous(sec.axis = sec_axis(~., name = 'drink')) +
scale_x_discrete(breaks = paste0(2009:2015, '0101')) +
theme(axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.x = element_text(size = rel(2)),
axis.text.y = element_text(size = rel(2)),
legend.position = c(1,0),
legend.justification = c(1,0),
legend.background = element_rect(size=rel(2)),
legend.title = element_blank(),
legend.text = element_text(size=rel(2))) +
geom_vline(xintercept = which(wom09$month %in% paste0(rep(2009:2015, 4), c('0301', '0601', '0901', '1201'))),
linetype='dashed', alpha=.2, size=.05) +
geom_vline(xintercept = which(wom09$month %in% paste0(rep(2009:2015), '0101')))
```
##09년 이후 / 여자 / BMI & 운동량
```{r echo=F, fig.height = 7, fig.width = 18}
ggplot(wom09,
aes(x=as.factor(month), y=scale(BMI)+2, group=1, colour='BMI')) +
geom_line(size=1.5) +
geom_line(aes(y=scale(workout), colour='workout'), size=1.5) +
theme_minimal() +
scale_y_continuous(sec.axis = sec_axis(~., name = 'workout')) +
scale_x_discrete(breaks = paste0(2009:2015, '0101')) +
theme(axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.x = element_text(size = rel(2)),
axis.text.y = element_text(size = rel(2)),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
legend.position = c(1,0),
legend.justification = c(1,0),
legend.background = element_rect(size=rel(2)),
legend.title = element_blank(),
legend.text = element_text(size=rel(2))) +
geom_vline(xintercept = which(wom09$month %in% paste0(rep(2009:2015, 4), c('0301', '0601', '0901', '1201'))),
linetype='dashed', alpha=.2, size=.05) +
geom_vline(xintercept = which(wom09$month %in% paste0(rep(2009:2015), '0101')))
```
##09년 이후 / 남자 / 키 & 체중 & BMI
```{r echo=F, fig.height = 7, fig.width = 18}
men09 <- l1 %>%
filter(year == 'latter' & sex == 'men') %>%
mutate(month = substr(as.character(day), 1, 6)) %>%
group_by(month) %>%
summarise(weight = mean(weight),
height = mean(height),
BMI = mean(BMI),
drink = mean(drink),
workout = mean(workout)) %>%
mutate(month = paste0(month, '01'))
ggplot(men09, aes(x = as.factor(month))) +
geom_line(aes(y = scale(height) +1, group = 1, colour = 'height'), size=1.5) +
geom_line(aes(y = scale(weight) -2, group = 2, colour = 'weight'), size=1.5) +
geom_line(aes(y = scale(BMI) + 3, group = 3, colour = 'BMI'), size=1.5) +
theme_minimal() +
scale_x_discrete(breaks = paste0(2009:2015, '0101')) +
theme(axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.x = element_text(size = rel(2)),
axis.text.y = element_text(size = rel(2)),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
legend.position = c(1,0),
legend.justification = c(1,0),
legend.background = element_rect(size=rel(2)),
legend.title = element_blank(),
legend.text = element_text(size=rel(4)),
legend.key.size = unit(1, 'cm')) +
geom_vline(xintercept = which(wom09$month %in% paste0(rep(2009:2015, 4), c('0301', '0601', '0901', '1201'))),
linetype='dashed', alpha=.2, size=.05) +
geom_vline(xintercept = which(wom09$month %in% paste0(rep(2009:2015), '0101'))) +
ggtitle('변수의 패턴 간 연관성을 보기위해 scaling')
```
##09년 이후 / 남자 / 체중 & 음주량
```{r echo=F, fig.height = 7, fig.width = 18}
ggplot(men09,
aes(x=as.factor(month), y=scale(weight), group=1, colour='weight')) +
geom_line(size=1.5) +
geom_line(aes(y=scale(drink) + 2, colour='drink'), size=1.5) +
theme_minimal() +
scale_y_continuous(sec.axis = sec_axis(~., name = 'drink')) +
scale_x_discrete(breaks = paste0(2009:2015, '0101')) +
theme(axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.x = element_text(size = rel(2)),
axis.text.y = element_text(size = rel(2)),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
legend.position = c(1,0),
legend.justification = c(1,0),
legend.background = element_rect(size=rel(2)),
legend.title = element_blank(),
legend.text = element_text(size=rel(2))) +
geom_vline(xintercept = which(men09$month %in% paste0(rep(2009:2015, 4), c('0301', '0601', '0901', '1201'))),
linetype='dashed', alpha=.2, size=.05) +
geom_vline(xintercept = which(men09$month %in% paste0(rep(2009:2015), '0101')))
```
##09년 이후 / 남자 / 체중 & 운동량
```{r echo=F, fig.height = 7, fig.width = 18}
ggplot(men09,
aes(x=as.factor(month), y=scale(weight)+2, group=1, colour='weight')) +
geom_line(size=1.5) +
geom_line(aes(y=scale(workout), colour='workout'), size=1.5) +
theme_minimal() +
scale_y_continuous(sec.axis = sec_axis(~., name = 'workout')) +
scale_x_discrete(breaks = paste0(2009:2015, '0101')) +
theme(axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.x = element_text(size = rel(2)),
axis.text.y = element_text(size = rel(2)),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
legend.position = c(1,0),
legend.justification = c(1,0),
legend.background = element_rect(size=rel(2)),
legend.title = element_blank(),
legend.text = element_text(size=rel(2))) +
geom_vline(xintercept = which(men09$month %in% paste0(rep(2009:2015, 4), c('0301', '0601', '0901', '1201'))),
linetype='dashed', alpha=.2, size=.05) +
geom_vline(xintercept = which(men09$month %in% paste0(rep(2009:2015), '0101')))
```
##09년 이후 / 남자 / BMI & 음주량
```{r echo=F, fig.height = 7, fig.width = 18}
ggplot(men09,
aes(x=as.factor(month), y=scale(BMI)+3, group=1, colour='BMI')) +
geom_line(size=1.5) +
geom_line(aes(y=scale(drink), colour='drink'), size=1.5) +
theme_minimal() +
scale_y_continuous(sec.axis = sec_axis(~., name = 'drink')) +
scale_x_discrete(breaks = paste0(2009:2015, '0101')) +
theme(axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.x = element_text(size = rel(2)),
axis.text.y = element_text(size = rel(2)),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
legend.position = c(1,0.5),
legend.justification = c(1,0.5),
legend.background = element_rect(size=rel(2)),
legend.title = element_blank(),
legend.text = element_text(size=rel(2))) +
geom_vline(xintercept = which(men09$month %in% paste0(rep(2009:2015, 4), c('0301', '0601', '0901', '1201'))),
linetype='dashed', alpha=.2, size=.05) +
geom_vline(xintercept = which(men09$month %in% paste0(rep(2009:2015), '0101')))
```
##09년 이후 / 남자 / BMI & 운동량
```{r echo=F, fig.height = 7, fig.width = 18}
ggplot(men09,
aes(x=as.factor(month), y=scale(BMI)+2, group=1, colour='BMI')) +
geom_line(size=1.5) +
geom_line(aes(y=scale(workout), colour='workout'), size=1.5) +
theme_minimal() +
scale_y_continuous(sec.axis = sec_axis(~., name = 'workout')) +
scale_x_discrete(breaks = paste0(2009:2015, '0101')) +
theme(axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.x = element_text(size = rel(2)),
axis.text.y = element_text(size = rel(2)),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
legend.position = c(1,0),
legend.justification = c(1,0),
legend.background = element_rect(size=rel(2)),
legend.title = element_blank(),
legend.text = element_text(size=rel(2))) +
geom_vline(xintercept = which(men09$month %in% paste0(rep(2009:2015, 4), c('0301', '0601', '0901', '1201'))),
linetype='dashed', alpha=.2, size=.05) +
geom_vline(xintercept = which(men09$month %in% paste0(rep(2009:2015), '0101')))
```