Created
April 26, 2020 00:46
-
-
Save simecek/4c41f1972b7ade060a3278e37253c3cc to your computer and use it in GitHub Desktop.
Comparison to conversions to recovered (20 days gap, ggflags, gganimate)
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(tidyverse) | |
| library(countrycode) # to counvert country names to 2 letter codes | |
| library(ggflags) # for flags | |
| library(gganimate) # for video | |
| # Getting data ------------------------------------------------------------ | |
| selected_countries <- c("Italy", "Spain", "France", "Germany", | |
| "Portugal", "United States", "Slovakia", "South Korea", "Japan", | |
| 'Belgium', 'Sweden', | |
| 'Switzerland', 'Denmark', 'Austria', 'Slovenia', 'Greece', | |
| 'Czechia', 'Romania', 'Ireland', 'Poland', 'Chile', | |
| 'Ukraine', 'Hungary', 'Finland', 'Albania', 'Bulgaria') | |
| url_deaths <- "http://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_global.csv" | |
| deaths <- read_csv(url_deaths) %>% filter(`Country/Region` %in% selected_countries, is.na(`Province/State`)) | |
| url_confirmed <- "http://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv" | |
| confirmed <- read_csv(url_confirmed) %>% filter(`Country/Region` %in% selected_countries, is.na(`Province/State`)) | |
| url_recovered <- "http://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_recovered_global.csv" | |
| recovered <- read_csv(url_recovered) %>% filter(`Country/Region` %in% selected_countries, is.na(`Province/State`)) | |
| # Comparison -------------------------------------------------------------- | |
| date1 <- "4/4/20" | |
| active_cases = confirmed[[date1]] - deaths[[date1]] - recovered[[date1]] | |
| active_cases | |
| date2 <- "4/24/20" | |
| new_recovered = recovered[[date2]] - recovered[[date1]] | |
| new_recovered | |
| cor(active_cases, new_recovered, method="spearman") | |
| plot(log(active_cases), log(new_recovered)) | |
| abline(0,1, col="red", lty=2) | |
| # data for plot | |
| dt <- tibble(country = recovered$`Country/Region`, new_recovered = new_recovered, | |
| active_cases = active_cases) | |
| # adding 2 letter codes | |
| dt$code <- countrycode(dt$country, origin = 'country.name', destination = 'iso2c') | |
| # Plot -------------------------------------------------------------------- | |
| ggplot(dt, aes(x=active_cases, y=new_recovered, label=code)) + | |
| geom_label() + | |
| scale_x_log10(limits=c(1e2, 1.1e5)) + | |
| scale_y_log10(limits=c(1e2, 1.1e5)) + | |
| geom_abline(slope=1, intercept=0, color="red", lty=2) + | |
| labs(x = 'Active cases 20 days ago', | |
| y = 'Recovered during the last 20 days', | |
| title = 'Conversion of active cases to recovered in 20 days', | |
| subtitle = date2) | |
| ggsave('cured_in_20days.png', width = 6, height = 6) | |
| # Fun with flags ---------------------------------------------------------- | |
| ggplot(dt, aes(x=active_cases, y=new_recovered, country=tolower(code))) + | |
| geom_flag(size=7.5) + | |
| scale_x_log10(limits=c(1e2, 1.1e5)) + | |
| scale_y_log10(limits=c(1e2, 1.1e5)) + | |
| geom_abline(slope=1, intercept=0, color="red", lty=2) + | |
| labs(x = 'Active cases 20 days ago', | |
| y = 'Recovered during the last 20 days', | |
| subtitle = 'Conversion of active cases to recovered in 20 days', | |
| title = date2) + | |
| theme_bw() | |
| # Video ------------------------------------------------------------------- | |
| dt_time = NULL | |
| # data | |
| for (i in 74:(ncol(deaths)-20)) { | |
| date1 <- names(deaths)[i] | |
| active_cases = confirmed[[date1]] - deaths[[date1]] - recovered[[date1]] | |
| date2 <- names(deaths)[i+20] | |
| new_recovered = recovered[[date2]] - recovered[[date1]] | |
| # data for plot | |
| dt <- data.frame(date = lubridate::mdy(date2), | |
| country = recovered$`Country/Region`, new_recovered = new_recovered, | |
| active_cases = active_cases) | |
| dt$code <- countrycode(dt$country, origin = 'country.name', destination = 'iso2c') | |
| dt_time <- rbind(dt_time, dt) | |
| } | |
| # static plot | |
| p = ggplot(dt_time, aes(x=active_cases, y=new_recovered, country=tolower(code))) + | |
| geom_flag(size=7.5) + | |
| scale_x_log10(limits=c(1e2, 1.1e5)) + | |
| scale_y_log10(limits=c(1e2, 1.1e5)) + | |
| geom_abline(slope=1, intercept=0, color="red", lty=2) + | |
| labs(x = 'Active cases 20 days ago', | |
| y = 'Recovered during the last 20 days', | |
| subtitle = 'Conversion of active cases to recovered (20 days gap)') | |
| p | |
| # video | |
| p + transition_time(date) + | |
| labs(title = "Date: {frame_time}") + | |
| theme_bw() | |
| anim_save(filename = "cured_in_20days.gif") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment