Last active
April 4, 2018 05:37
-
-
Save samclifford/6c6e675fa2d9201e1f60b644b739bfd3 to your computer and use it in GitHub Desktop.
#tidytuesday for US tuition data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
# download from https://github.com/rfordatascience/tidytuesday/blob/master/data/us_avg_tuition.xlsx | |
dat <- read_xlsx("us_avg_tuition.xlsx") %>% | |
gather(Year, Tuition, -State) %>% | |
separate(col = Year, into = c("Start", "End"), sep = "-") %>% | |
mutate_at(.vars = c("Start", "End"), .funs = parse_number) %>% | |
mutate(End = End + 2000) | |
states <- data.frame(State = state.name, state=state.abb) | |
dat %<>% inner_join(states) | |
rates <- dat %>% group_by(State) %>% | |
nest %>% | |
mutate(model = map(data, | |
~glm(data = ., | |
round(Tuition) ~ Start , | |
family = poisson() | |
))) %>% | |
unnest(model %>% map(broom::tidy)) %>% | |
filter(term == "Start") %>% | |
mutate(Rate = exp(estimate) - 1) %>% | |
select(State, Rate) %>% | |
inner_join(states) | |
# devtools::install_github("hafen/geofacet") | |
library(geofacet) | |
to_plot <- dat %>% | |
inner_join(rates) %>% | |
mutate(Rate = 100*Rate) %>% | |
mutate(rate_cut = cut(Rate, | |
breaks = seq(floor(min(Rate)), | |
ceiling(max(Rate))+1, | |
by = 2))) %>% | |
mutate(Start_ymd = parse_date(paste(Start, "01", "01", sep="-"))) | |
us_state_grid2a <- filter(us_state_grid2, code != "DC") %>% | |
mutate(row = ifelse(code == "MD", row - 1, row)) | |
pdf(file = "states_trends.pdf", width = 10, height = 7) | |
ggplot(to_plot, aes(Start_ymd, Tuition/1000)) + | |
geom_line(data=select(to_plot, -state), aes(group=State), alpha=0.025) + | |
geom_line(aes(color=rate_cut), size=1) + | |
facet_geo(~ state, grid = us_state_grid2a) + | |
ylim(c(0,NA)) + | |
xlab("Academic Year Start") + | |
scale_x_date(date_labels = "'%y") + | |
scale_color_manual(values=RColorBrewer::brewer.pal(length(levels(to_plot$rate_cut))+1,"Reds")[-1], | |
name="Annual average percent increase in tuition") + | |
ylab("Annual Tuition (1000 USD)") + | |
theme_bw() + | |
theme(legend.position = "bottom", panel.grid.minor = element_blank()) + | |
labs(title="Rising tuition costs at four year public colleges", | |
subtitle="Data from College Trends (2015)") | |
dev.off() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment