Skip to content

Instantly share code, notes, and snippets.

@acoppock
Created Mar 15, 2020
Embed
What would you like to do?
rm(list = ls())
library(tidyverse)
library(ggrepel)
covid <-
read_csv(file = "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-Confirmed.csv")
covid <-
covid %>% filter(`Country/Region` != "Cruise Ship")
covid_long <-
covid %>%
pivot_longer(names_to = "date",
values_to = "cases",
cols = ends_with("20")) %>%
mutate(date = as.Date(date, "%m/%d/%y")) %>%
group_by(date, `Country/Region`) %>%
summarise(cases = sum(cases))
covid_long <-
covid_long %>%
group_by(`Country/Region`) %>%
mutate(seen_100_cases = max(cases) > 100) %>%
filter(seen_100_cases) %>%
mutate(dayof100 = min(date[cases > 100]),
days_since_100 = date - dayof100) %>%
filter(days_since_100 > 0)
label_df <-
covid_long %>%
group_by(`Country/Region`) %>%
arrange(-days_since_100) %>%
slice(1) %>%
filter(days_since_100 > 5)
thirty_three <- function(x)
100 * (4 / 3) ^ x
line_df <-
tibble(days_since_100 = 1:50,
cases = thirty_three(days_since_100))
g <-
ggplot(label_df, aes(days_since_100, cases, group = `Country/Region`)) +
geom_text_repel(aes(label = `Country/Region`)) +
geom_point() +
geom_line(alpha = 0.25, data = covid_long) +
geom_line(alpha = 0.25,
linetype = "dashed",
aes(group = NULL),
data = line_df) +
scale_y_log10(labels = comma) +
annotate("text", 30, thirty_three(30), label = "33% daily increase") +
theme_bw() +
coord_cartesian(ylim = c(100, 1000000)) +
labs(
y = "Confirmed cases",
x = "Days since 100th case reported",
caption = "Data: https://github.com/CSSEGISandData/COVID-19
Code: ",
title = "Cumulative cases since 100th case reported",
subtitle = "Graph design inspired by @jburnmurdoch
https://www.ft.com/content/a26fbf7e-48f8-11ea-aeb3-955839e06441"
)
ggsave(
filename = "covid_graph.pdf",
plot = g,
width = 6.5,
height = 4.5
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment