if (!require(pacman)) {
install.packages("pacman")
require(pacman)
}
p_load(lubridate, timetk, tidyverse, knitr, ggthemes, httr, noncensus, cowplot, readxl, flexdashboard)
p_load_gh("hrbrmstr/hrbrthemes","jbkunst/highcharter")
data("states")
path <- "https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/us_avg_tuition.xlsx"
GET(path, write_disk(tf <- tempfile(fileext = ".xlsx")))Response [https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/us_avg_tuition.xlsx]
Date: 2018-08-09 02:52
Status: 200
Content-Type: application/octet-stream
Size: 23 kB
<ON DISK> /var/folders/_3/krqnmsjd3xg6xmwzck24hln40000gn/T//RtmpOPGdfd/file34683c30e6f.xlsx
dat <- read_excel(tf) %>%
mutate(Divsion = states[match(State, states$name), 4],
Region = states[match(State, states$name), 3],
State = states[match(State, states$name), 1]) %>%
gather(Year, Tuition, -c(State, Region, Divsion)) %>%
mutate(Year = as.numeric(gsub("-.*", "", Year))) %>%
mutate_if(is.numeric, round, digits=3) %>%
{.}
unlink(tf)ggplot2:
st_plt <- function(df, divsion) {
df %>%
filter(Divsion == divsion) %>%
ggplot(aes(Year, Tuition, color = State, fill = State)) +
geom_line(stat = "identity", size = .7) +
theme_ipsum_rc() +
ylim(c(3500,15500)) +
coord_fixed(ratio = .00065) +
xlab("") + ylab("")
}
plist <- lapply(levels(dat$Divsion),
FUN = function(x) {st_plt(dat,x)})highcharter:
hc_st_plt <- function(df, divsion) {
df %>%
filter(Divsion == divsion) %>%
hchart("spline", hcaes(x = Year, y = Tuition, group = State)) %>%
hc_tooltip(crosshairs = TRUE, shared = TRUE)
}