Skip to content

Instantly share code, notes, and snippets.

@romainfrancois
Last active October 16, 2017 20:54
Show Gist options
  • Save romainfrancois/3c932458d2d51a39ddbc8a0a198cd662 to your computer and use it in GitHub Desktop.
Save romainfrancois/3c932458d2d51a39ddbc8a0a198cd662 to your computer and use it in GitHub Desktop.
library(tidyverse)
library(scales)
# bc who column names are weird
parse_percentile <- function(x){
x <- str_replace( x, "^P", "" )
case_when(
x == "01" ~ .1,
x == "999" ~ 99.9,
TRUE ~ as.numeric(x)
)
}
url <- "http://www.who.int/childgrowth/standards/wfa_girls_p_exp.txt"
baby_girls_weight <- read_delim(url, delim = "\t" ) %>%
select( -(L:S) ) %>%
gather( percentile, weight, -Age) %>%
mutate( percentile = parse_percentile(percentile) )
scale_color_gradient_mirror <- function (..., colors ,
midpoint = 0, space = "Lab", na.value = "grey50", guide = "colourbar") {
cols <- c( head(colors, -1), tail( colors, 1), rev( head( colors, -1)) )
continuous_scale("colour", "gradient2",
gradient_n_pal( cols ),
na.value = na.value, guide = guide,
..., rescaler = ggplot2:::mid_rescaler(mid = midpoint))
}
ggplot( filter(baby_girls_weight, Age < 100) ) +
scale_color_gradient_mirror(midpoint=50, colors = c("pink", "purple", "blue") ) +
aes( Age, weight, color = percentile, group = percentile ) +
geom_line( ) +
xlab( "Days" ) +
ylab( "Weight (kg)") +
theme_light() +
geom_text(
data = filter(baby_girls_weight, Age == 100),
mapping = aes( Age + 2 , weight, label = percentile, color = NULL ),
size = 2
) +
geom_text(
data = filter(baby_girls_weight, Age == 0),
mapping = aes( Age - 2 , weight, label = percentile, color = NULL ),
size = 2
) +
guides(colour=FALSE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment