Skip to content

Instantly share code, notes, and snippets.

@johnburnmurdoch
Created February 13, 2020 17:27
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save johnburnmurdoch/bb2342ea81d3b1ce598084cfc626f390 to your computer and use it in GitHub Desktop.
Save johnburnmurdoch/bb2342ea81d3b1ce598084cfc626f390 to your computer and use it in GitHub Desktop.
install.packages("needs")
library(needs)
needs(tidyverse, magrittr, rvest, zoo, scales)
WHO_sars_links <- read_html("https://www.who.int/csr/sars/country/en/") %>%
html_nodes("ul.auto_archive") %>%
magrittr::extract(1) %>%
html_nodes("li a") %>%
map_dfr(~{
link <- .x %>% html_attr("href") %>% paste0("https://www.who.int",.)
date <- .x %>% html_text(T) %>% as.Date("%d %B %Y")
tibble(date, link)
})
WHO_sars_links %>%
split(.$date) %>%
imap_dfr(~{
message(.y)
page <- read_html(.x$link)
final_row <- page %>% html_nodes("div#primary table tr:nth-last-child(1)")
final_row <- final_row %>% html_text(T)
final_row <- final_row %>% keep(~ grepl("Total",.x))
total <- final_row %>% str_extract("\\d{3,4}") %>% parse_number()
tibble(date = .x$date, total)
}) -> WHO_sars_totals
covid_totals <- read_html("https://www.worldometers.info/coronavirus/coronavirus-cases/") %>%
html_nodes("body > div.container > div:nth-child(2) > div.col-md-8 > div > div:nth-child(17) > div:nth-child(2) > div > table tbody tr") %>%
map_dfr(~{
date <- .x %>% html_nodes("td:nth-child(1)") %>% html_text(T) %>% paste(., " 2020") %>% as.Date("%b. %d %Y")
total <- .x %>% html_nodes("td:nth-child(2)") %>% html_text(T) %>% parse_number()
tibble(date, total)
})
bind_rows(
WHO_sars_totals %>%
mutate(days = date - as.Date("2002-11-27")) %>%
bind_rows(tibble(days = 0, total = 0),.) %>%
filter(total <= 8096 & days <= 200) %>%
complete(days = full_seq(days, 1)) %>%
mutate(total = na.locf(total), virus = "SARS"),
covid_totals %>%
mutate(days = date - as.Date("2019-12-01")) %>%
bind_rows(tibble(days = 0, total = 0),.) %>%
complete(days = full_seq(days, 1)) %>%
mutate(total = na.locf(total), virus = "Covid-19")
) %>%
ggplot(aes(days, total, col = virus)) +
geom_step(direction = "hv", size = 0.8) +
geom_point(data = . %>% group_by(virus) %>% top_n(1, days)) +
geom_text(data = . %>% group_by(virus) %>% top_n(1, days), aes(label = paste0(" ",virus)), hjust=0, family = "Open Sans", fontface = "bold") +
theme_minimal() +
theme(
plot.title.position = "plot",
plot.title = element_textbox_simple(
size = 13,
lineheight = 1.2,
margin = margin(5, 0, 5.5, 0),
padding = margin(0,0,8,0)
),
axis.title.x = element_textbox_simple(
size = 10,
lineheight = 1.2,
margin = margin(5, 0, 5.5, 0),
padding = margin(0,0,8,0),
hjust = 0.5
),
panel.grid.minor = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.major.y = element_line(color = c("black", rep("gray90", 10))),
axis.ticks.x = element_line(),
legend.position = "none",
plot.margin = unit(c(2,15,2,2), "mm"),
text = element_text(family = "Open Sans"),
axis.title.y = element_blank(),
plot.caption.position = "plot",
plot.caption = element_textbox_simple(
size = 8,
lineheight = 1.1,
margin = margin(0, 0, 0, 0),
padding = margin(7,0,0,0),
hjust=0
)
) +
scale_colour_manual(values = c("red", "cyan3")) +
coord_cartesian(clip = "off") +
scale_x_continuous(expand = c(0,0)) +
scale_y_continuous(expand = c(0,0), labels = label_comma()) +
labs(x = "Days since outbreak began <span style='font-family: Arial'>&rarr;</span>", y = "", title = "<b style='font-weight:bold; color:red'>Covid-19 (Coronavirus)</b> has infected\n far more people than <b style='font-weight:bold; color:cyan3'>SARS</b> did at the same stage", caption = "Sources: WHO; Worldometer")
ggsave("covid.png", scale=2, width = 7, height = 5, units = "cm")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment