Skip to content

Instantly share code, notes, and snippets.

@johnbaums
Created April 4, 2020 06:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnbaums/4cecebafeec0102a7e282ac06b56a787 to your computer and use it in GitHub Desktop.
Save johnbaums/4cecebafeec0102a7e282ac06b56a787 to your computer and use it in GitHub Desktop.
Confirmed cases of COVID-19, country comparison (https://public.flourish.studio/visualisation/1619458/embed)
library(jsonlite)
library(xml2)
library(rvest)
library(dplyr)
library(tidyr)
library(ggplot2)
library(plotly)
d <- xml2::read_html('https://public.flourish.studio/visualisation/1619458/embed')
js <- rvest::xml_nodes(d, 'script') %>% html_text
js <- js[grep('_Flourish_data_column_names', js)] %>% strsplit('\n') %>% .[[1]]
labels <- fromJSON(gsub('var _Flourish_data_column_names = |,$', '',
js[grep('_Flourish_data_column_names =', js)][1]))
values <- fromJSON(gsub('_Flourish_data = |;$', '',
js[grep('_Flourish_data =', js)][1]))$data
dat <- do.call(rbind, setNames(values$value, values$label)) %>%
as.data.frame(stringsAsFactors=FALSE) %>%
setNames(labels$data$value) %>%
dplyr::as.tbl() %>%
dplyr::mutate_all(as.numeric) %>%
dplyr::mutate(day=dplyr::row_number()) %>%
dplyr::select(day, everything()) %>%
tidyr::pivot_longer(-day, names_to='country', values_to='cases')
# cumulative cases since first infection (raw)
(ggplot(dat, aes(x=day, y=cases, color=country)) +
geom_line() +
xlab('Days since first case') +
ylab('Total confirmed cases')) %>%
ggplotly
# cumulative cases since first infection (log10)
(ggplot(dat, aes(x=day, y=log10(cases), color=country)) +
geom_line() +
xlab('Days since first case') +
ylab('log10(Total confirmed cases)')) %>%
ggplotly
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment