Skip to content

Instantly share code, notes, and snippets.

@christophsax
Last active April 5, 2020 20:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christophsax/dec0a57bcbc9d7517b852dd44eb8b20b to your computer and use it in GitHub Desktop.
Save christophsax/dec0a57bcbc9d7517b852dd44eb8b20b to your computer and use it in GitHub Desktop.
Coronavirus disease: new infections and total cases
library(tidyverse)
library(anytime)

# Data Repo Johns Hopkins CSSE
# https://github.com/CSSEGISandData/COVID-19
url <- "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv"
dta_raw <- read_csv(url, col_types = cols()) %>%
  select(-Lat, -Long)

# grep("Uni", unique(dta_raw$`Country/Region`), v = T)
selection <- c("China", "Italy",  "Switzerland", "Korea, South", "France", "Germany", "Austria", "US", "United Kingdom")

dta <-
  dta_raw %>%

  # tidy data
  rename(province = `Province/State`, country = `Country/Region`) %>%
  pivot_longer(c(-province, -country), "time") %>%
  mutate(time = as.Date(time, "%m/%d/%y")) %>%

  # selection
  filter(country %in% !! selection) %>%

  # ignore provinces
  group_by(country, time) %>%
  summarize(value = sum(value)) %>%
  ungroup() %>%

  # calculate new confirmed cases
  arrange(time) %>%
  group_by(country) %>%
  mutate(diff = value - lag(value)) %>%
  ungroup() %>%
  filter(!is.na(diff)) %>%
  arrange(country, time)

dta %>%
  filter(diff > 0) %>%
  ggplot(aes(x = time, y = diff, fill = country %in% c("China", "Korea, South"))) +
  geom_col(show.legend = FALSE) +
  facet_wrap(vars(country), scales = "free_y") +
  ggtitle("New confirmed cases", "Coronavirus disease (COVID-19), log scale")+
  scale_y_continuous(trans='log10') +
  theme_minimal()

Created on 2020-04-05 by the reprex package (v0.3.0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment