Created
February 17, 2020 22:00
-
-
Save jbkunst/2e612b35704f3e6c4e16f5fce87ed675 to your computer and use it in GitHub Desktop.
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(lubridate) | |
library(highcharter) | |
confirmed <- read_csv("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-Confirmed.csv") | |
get_data <- function(url = "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-Deaths.csv"){ | |
data_wide <- read_csv(url) | |
data_long <- data_wide %>% | |
gather(date, value, -`Province/State`, -`Country/Region`, -Lat, -Long) %>% | |
rename_all(str_to_lower) %>% | |
rename_all(str_replace_all, "/", "_") %>% | |
mutate(date = mdy(str_extract(date, "[0-9]+/[0-9]+/[0-9]+"))) %>% | |
group_by(province_state, country_region, lat, long, date) %>% | |
summarise(value = max(value)) %>% | |
ungroup() %>% | |
arrange(province_state, date) %>% | |
mutate(value = ifelse(value == 0, NA, value)) | |
dmap <- data_long %>% | |
group_by(province_state, country_region, lat, long) %>% | |
summarise(sequence = list(value)) %>% | |
ungroup() | |
dmap | |
} | |
confmd <- get_data("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-Confirmed.csv") | |
deaths <- get_data("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-Deaths.csv") | |
rcvred <- get_data("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-Recovered.csv") | |
data <- list( | |
confmd, | |
deaths, | |
rcvred | |
) %>% | |
map2_df( | |
c("confirmed", "deaths", "recovered"), | |
~ mutate(.x, category = .y) | |
) %>% | |
unnest(sequence) | |
dates <- data %>% | |
distinct(date) %>% | |
arrange(date) %>% | |
pull() | |
dates <- confirmed %>% | |
select(5:last_col()) %>% | |
names() %>% | |
mdy() | |
auxseq <- data %>% | |
summarise(max(sequence, na.rm = TRUE)) %>% | |
pull() %>% | |
rep(times = length(dates)) | |
aux <- tibble( | |
province_state = "false", | |
name = "false", | |
lat = NA, | |
long = NA, | |
sequence = list(auxseq) | |
) | |
aux | |
hcmap(showInLegend = FALSE) %>% | |
hc_add_series( | |
data = confmd, | |
type = "mapbubble", | |
name= "Confirmed", | |
hcaes(lat = lat, lon = long, size = sequence, name = paste(country_region, province_state)), | |
color = "yellow" | |
) %>% | |
hc_add_series( | |
data = deaths, | |
type = "mapbubble", | |
name= "Deaths", | |
hcaes(lat = lat, lon = long, size = sequence, name = paste(country_region, province_state)), | |
color = "red" | |
) %>% | |
hc_add_series( | |
data = rcvred, | |
type = "mapbubble", | |
name= "Recovered", | |
hcaes(lat = lat, lon = long, size = sequence, name = paste(country_region, province_state)), | |
color = "green" | |
) %>% | |
hc_add_series( | |
data = auxiliar, | |
type = "mapbubble", | |
name= "auxiliar", | |
hcaes(lat = lat, lon = long, size = sequence, name = province_state), | |
enableMouseTracking= FALSE, | |
color = "transparent", | |
showInLegend = FALSE | |
) %>% | |
hc_mapNavigation(enabled = TRUE) %>% | |
hc_plotOptions( | |
bubble = list( | |
minSize = 0, | |
maxSize = 20, | |
sizeBy = "area" | |
) | |
) %>% | |
hc_motion( | |
enabled = TRUE, | |
series = c(1:4), | |
labels = dates, | |
autoPlay = FALSE, | |
magnet = list(step = 1), | |
updateInterval = 400 | |
) %>% | |
hc_tooltip(useHTML = TRUE) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment