415 字

# 生长曲线

### 头围

WHO的小孩生长数据在这里. 接下来我们把这些数据读入R，然后整理下。

# 读取数据

# 数据整理
names(length.day.girl)[1]="Age"
names(length.day.boy)[1]="Age"

weight.day.girl$type = "weight_kg" weight.day.boy$type = "weight_kg"
head.circ.day.girl$type = "head.circ_cm" head.circ.day.boy$type = "head.circ_cm"
length.day.girl$type = "length_cm" length.day.boy$type = "length_cm"

weight.day.girl$gender = "female" weight.day.boy$gender = "male"
head.circ.day.girl$gender = "female" head.circ.day.boy$gender = "male"
length.day.girl$gender = "female" length.day.boy$gender = "male"

wlh.perc = rbind(weight.day.girl,
weight.day.boy,
length.day.girl,
length.day.boy)
names(wlh.perc)[1] = "Age_day"
names(wlh.perc) # L: Box-cox power, M: median, S: coef of variance

# 需要的R包
library(reshape2)
library(plyr)
library(dplyr)
library(stringr)

wlh.perc = select(wlh.perc, -c(L, M, S))
wlh.perc.long = melt(wlh.perc, id.vars = c(1, 17, 18), variable.name = "percentile")



### Zoey的数据整理。

zoey = read.csv("Zoey_growth.csv", na.string = "0 cm", stringsAsFactors = F)
zoey$Time = as.Date(ldply(str_split(zoey$Time, pattern = ","))[,1], format = "%m/%d/%y")
zoey$day = as.numeric(zoey$Time - as.Date("2014-09-15"))
zoey$Length = as.numeric(str_sub(zoey$Length, 1, -4))
zoey$Head.Size = as.numeric(str_sub(zoey$Head.Size, 1, -4))
pod = as.numeric(str_extract(string = zoey$Weight, pattern = "^[0-9]*")) ounce = as.numeric(str_extract(str_extract(string = zoey$Weight, pattern = "([0-9]*) oz.$"), pattern = "^[0-9]*")) ounce[is.na(ounce)] = 0 zoey$weight_kg = pod/2.2046 + ounce/35.274


### 画图

library(ggplot2); theme_set(theme_bw())

days = 0:60
quantile = c("P1", "P25","P50","P75","P99")

# length
height = ggplot(filter(wlh.perc.long, percentile %in% quantile,
Age_day %in% days, type == "length_cm", gender == "female"),
aes(x = Age_day, y = value)) +
geom_line(aes(color = percentile)) +
geom_point(data = na.omit(zoey[c("Length", "day")]), aes( x = day, y = Length))

headsize = ggplot(filter(wlh.perc.long, percentile %in% quantile,
Age_day %in% days, type == "head.circ_cm", gender == "female"),
aes(x = Age_day, y = value)) +
geom_line(aes(color = percentile)) +