Created
January 12, 2017 17:34
-
-
Save elliottmorris/0bfefd2807abdd4561901dc03e6b603f to your computer and use it in GitHub Desktop.
A short intro to 538's r package
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
################################### | |
# exploring the 538 package | |
# code by G. Elliott Morris, @gelliottmorris | |
################################### | |
#install pacakges | |
install.packages(c("fivethirtyeight","ggplot2","dplyr","ggthemes")) | |
#load them | |
library(fivethirtyeight) | |
library(ggplot2) | |
library(dplyr) | |
library(ggthemes) | |
################################## | |
# some data | |
################################## | |
# get the fivethirtyeight data for 2015 NBA player pedictions | |
nba <- nba_draft_2015 | |
names(nba) | |
#group the statistical plut-minus by year | |
nba_yearly <- nba %>% group_by(draft_year) %>% summarise(projected_spm = mean(projected_spm)) | |
################################## | |
# graph it! | |
################################## | |
gg <- ggplot(nba_yearly, aes(x = draft_year, y = projected_spm,fill = projected_spm)) + | |
geom_bar(stat = 'identity') + | |
theme_fivethirtyeight() + | |
labs(title = "When is the NBA at its Best?", | |
subtitle = "As measured by the average player's projected plus-minus each year", | |
caption = "Data from FiveThirtyEight") + | |
theme(legend.position = "none", | |
plot.title = element_text(face = "bold", size = 20), | |
plot.subtitle = element_text(size = 12), | |
plot.caption= element_text(hjust = 0, size = 10)) | |
################################## | |
# output it! | |
################################## | |
png("NBAbest.png", width = 8, height = 6, unit = "in", res = 200) | |
gg | |
dev.off() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment