Skip to content

Instantly share code, notes, and snippets.

@javierluraschi
Last active April 22, 2020 15:32
Show Gist options
  • Save javierluraschi/cd4143e329f92bd7c4207c70265c67e7 to your computer and use it in GitHub Desktop.
Save javierluraschi/cd4143e329f92bd7c4207c70265c67e7 to your computer and use it in GitHub Desktop.
COVID-19:: Daily Confirmed Cases -- Worldwide and USA

COVID-19: Daily Confirmed Cases

Using data from RamiKrispin/coronavirus, first plot worldwide new cases for countries with more than a thousand cases:


title: "R Notebook" output: html_notebook

library(dplyr)
library(magrittr)
library(ggplot2)
library(magrittr)

load(pins::pin("https://github.com/RamiKrispin/coronavirus/raw/master/data/coronavirus.rda"))

regions <- filter(coronavirus, type == "confirmed") %>%
  group_by(`Country.Region`) %>%
  summarise(n = sum(cases)) %>%
  filter(n > 1000) %>%
  pull(`Country.Region`)

filter(coronavirus, type == "confirmed", `Country.Region` %in% regions) %>%
  group_by(date, `Country.Region`) %>%
  summarise(cases = sum(cases)) %>%
  ggplot(aes(x = date, y = cases)) +
    geom_point(size = 0.5) +
    geom_line(alpha = 0.3) +
    geom_smooth(alpha = 0.5, formula = y ~ x, method = "loess") +
    facet_wrap(~`Country.Region`, scales = "free") +
    ggtitle("COVID-19: Daily Confirmed Cases (Worldwide)", subtitle = "Regions with 1000 or more cases") +
    theme_light() +
    ggsave("~/RStudio/temp/covid19-daily-cases-world.png", device = "png", width = 20, height = 12)
filter(coronavirus, type == "death", `Country.Region` %in% regions) %>%
  group_by(date, `Country.Region`) %>%
  summarise(cases = sum(cases)) %>%
  ggplot(aes(x = date, y = cases)) +
    geom_point(size = 0.5) +
    geom_line(alpha = 0.3) +
    geom_smooth(alpha = 0.5, formula = y ~ x, method = "loess") +
    facet_wrap(~`Country.Region`, scales = "free") +
    ggtitle("COVID-19: Daily Deaths (Worldwide)", subtitle = "Regions with 1000 or more cases") +
    theme_light() +
    ggsave("~/RStudio/temp/covid19-daily-deaths-world.png", device = "png", width = 20, height = 12)
filter(coronavirus, type == "death", `Country.Region` %in% regions) %>%
  group_by(date, `Country.Region`) %>%
  summarise(cases = sum(cases)) %>%
  group_by(`Country.Region`) %>%
  mutate(cases = cumsum(cases)) %>%
  ggplot(aes(x = date, y = cases)) +
    geom_point(size = 0.5) +
    geom_line(alpha = 0.3) +
    geom_smooth(alpha = 0.5, formula = y ~ x, method = "loess") +
    facet_wrap(~`Country.Region`, scales = "free") +
    ggtitle("COVID-19: Cumulative Deaths (Worldwide)", subtitle = "Regions with 1000 or more cases") +
    theme_light() +
    ggsave("~/RStudio/temp/covid19-daily-cumdeaths-world.png", device = "png", width = 20, height = 12)
usa <- readr::read_csv(pins::pin("http://covidtracking.com/api/states/daily.csv", name = "covid-usa"))

filter(usa) %>%
  mutate(cases = positiveIncrease, date = as.Date(as.character(date), "%Y%m%d")) %>%
  ggplot(aes(x = date, y = cases)) +
    geom_point(size = 0.5) +
    geom_line(alpha = 0.3) +
    geom_smooth(alpha = 0.5, formula = y ~ x, method = "loess") +
    facet_wrap(~state, scales = "free") +
    ggtitle("COVID-19: Daily Confirmed Cases (USA)", subtitle = "Areas with 100 or more cases") +
    theme_light() +
    ggsave("~/RStudio/temp/covid19-daily-cases-usa.png", device = "png", width = 14, height = 8)
filter(usa) %>%
  mutate(cases = deathIncrease, date = as.Date(as.character(date), "%Y%m%d")) %>%
  ggplot(aes(x = date, y = cases)) +
    geom_point(size = 0.5) +
    geom_line(alpha = 0.3) +
    geom_smooth(alpha = 0.5, formula = y ~ x, method = "loess") +
    facet_wrap(~state, scales = "free") +
    ggtitle("COVID-19: Daily Deaths (USA)", subtitle = "Areas with 100 or more cases") +
    theme_light() +
    ggsave("~/RStudio/temp/covid19-daily-deaths-usa.png", device = "png", width = 14, height = 8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment