Skip to content

Instantly share code, notes, and snippets.

@semenoffalex
Last active October 14, 2017 14:00
Show Gist options
  • Save semenoffalex/9fdaadb03efd6dddd0b25fc53c06720b to your computer and use it in GitHub Desktop.
Save semenoffalex/9fdaadb03efd6dddd0b25fc53c06720b to your computer and use it in GitHub Desktop.
Code to create a Joy Division 'Unknown Pleasures' album cover with data from Dota 2 match duration by month
library(tidyverse)
library(jsonlite)
library(lubridate)
library(ggridges)
opendota_query <- function(sqlQuery) {
requestUrl = paste("https://api.opendota.com/api/explorer?sql=",
URLencode(sqlQuery), sep="")
result <- fromJSON(requestUrl)
return(result$rows)
}
q_dur <- "
SELECT
avg(duration)::int as time,
matches.match_id,
min(matches.start_time) as start_time,
max(patch) as patch
FROM matches
JOIN match_patch using(match_id)
WHERE TRUE
AND duration IS NOT NULL
GROUP BY match_id
ORDER BY time DESC NULLS LAST
"
dur <- opendota_query(q_dur) %>%
mutate(start_time = as.POSIXct(start_time, origin = "1970-01-01", tz = "UTC"),
month = month(start_time),
year = year(start_time),
minutes = time/60) %>%
mutate(yearmon = as.factor(paste0(month,'_',year)))
dur %>% filter(minutes <= 75) %>%
ggplot(aes(x = minutes, y = yearmon)) +
geom_density_ridges(fill = 'white', scale = 9) +
theme_ridges(grid = FALSE) +
scale_y_discrete(name = '', breaks = NULL, labels = NULL) +
scale_x_continuous(name = 'UNKNOWN PLEASURES', breaks = NULL, labels = NULL) +
ggtitle("JOY DIVISION") +
theme(plot.title = element_text(family = "Helvetica", size = (25), hjust = 0.5),
axis.title.x = element_text(family = "Arial", size = (25), hjust = 0.5))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment