Last active
June 8, 2019 10:50
-
-
Save semenoffalex/131c1a1e1bf9f1e922f9c94821bd9217 to your computer and use it in GitHub Desktop.
Trying to make a Wikipedia-style timeline in R
This file contains hidden or 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(rvest) | |
library(tidyverse) | |
library(lubridate) | |
library(scales) | |
library(Cairo) | |
library(lubridate) | |
url <- "https://liquipedia.net/dota2/Team_Secret" | |
page <- read_html(url) | |
tbls <- html_nodes(page, ".roster") %>% | |
html_nodes("table") %>% | |
html_table(fill = TRUE) | |
tbls_df <- lapply(tbls, function(x) x[-1, 1:5]) | |
tbls_df <- lapply(tbls_df, function(x) {colnames(x) = c("id", "name", "position", "join_date", | |
"leave_date"); x}) | |
tbl <- bind_rows(tbls_df) %>% | |
mutate(joined = ymd(substr(join_date, 12, 21)), | |
left = ymd(substr(leave_date, 13, 22))) %>% | |
select(-c(join_date, leave_date)) %>% | |
filter(name != 'distiled') | |
tbl$position <- ifelse(tbl$position %in% c("1", "1 (Sub)"), 1, | |
ifelse(tbl$position %in% c("1/2", "2"), 2, | |
ifelse(tbl$position %in% c("3"), 3, | |
ifelse(tbl$position %in% c("4", "4 (Sub)", "4/5"), 4, | |
ifelse(tbl$position %in% c("5", "5 (Sub)", "Sub"), 5, 5))))) | |
tbl$left <- ifelse(is.na(tbl$left), as.Date(18055, origin="1970-01-01"), tbl$left) %>% | |
as.Date(origin = "1970-01-01") | |
tbl_l <- tbl %>% | |
mutate(Start = as.Date(joined, "%Y-%m-%d"), | |
End = as.Date(left, "%Y-%m-%d")) %>% | |
gather(date.type, task.date, -c(id, name, position)) %>% | |
arrange(date.type, task.date) %>% | |
mutate(Task = factor(id, levels = rev(unique(id)), ordered = TRUE)) %>% | |
filter(name != c('distiled', "Solitude")) | |
timeline <- ggplot(tbl_l, aes(x = Task, y = task.date, colour = position)) + | |
geom_line(size = 6) + | |
guides(colour = guide_legend(title = NULL)) + | |
labs(x = NULL, y = NULL) + | |
coord_flip() + | |
scale_y_date() + | |
theme(axis.text.x = element_text(angle = 45, hjust = 1)) | |
timeline |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment