Created
May 14, 2019 22:22
-
-
Save tylerlittlefield/8a82016e817b4715c0da32d7c5600f6a to your computer and use it in GitHub Desktop.
Draft Lottery Team Records
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) | |
library(janitor) | |
library(glue) | |
library(rvest) | |
df <- "https://en.wikipedia.org/wiki/2019_NBA_draft" %>% | |
read_html() %>% | |
html_table(fill = TRUE, header = FALSE) %>% | |
.[[6]] %>% | |
as_tibble(.name_repair = "unique") %>% | |
set_names(.[2,]) %>% | |
clean_names("snake") %>% | |
.[-c(1:2), ] %>% | |
gather("round", "lottery_prob", -c(team, x2018_19record, lotterychances)) %>% | |
set_names("team", "record", "lottery_chances", "round", "lottery_prob") %>% | |
mutate(round = str_extract(round, "[[:digit:]]")) %>% | |
separate(record, c("wins", "losses"), sep = "–") %>% | |
mutate(team = str_extract(team, "[ [:alpha:]]*")) %>% | |
type_convert() | |
df %>% | |
select(team, wins, losses) %>% | |
distinct(team, .keep_all = TRUE) %>% | |
mutate(label = glue("{wins}/{losses}")) %>% | |
gather(key, value, -c(team, label)) %>% | |
arrange(desc(value)) %>% | |
mutate(team = as_factor(team)) %>% | |
ggplot(aes(team, value, fill = key)) + | |
geom_col(color = "black") + | |
labs(x = NULL, y = NULL, fill = NULL, | |
title = "Draft Lottery Team Records", | |
subtitle = "Wins and losses from 2018-2019 season") + | |
geom_hline(yintercept = 0, size = 1.5) + | |
geom_text(aes(team, 4, label = label), hjust = 0, size = 6) + | |
scale_y_continuous(expand = c(0, 0)) + | |
scale_fill_manual(values = c("#fc9272", "#99d8c9")) + | |
coord_flip() + | |
guides(fill = guide_legend(reverse = TRUE)) + | |
theme( | |
panel.background = element_blank(), | |
axis.ticks = element_blank(), | |
legend.position = "bottom", | |
text = element_text(family = "Helvetica", size = 20), | |
plot.title = element_text(size = 28, face = "bold"), | |
plot.caption = element_text(size = 12) | |
) |
Author
tylerlittlefield
commented
May 14, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment