Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ##############################################################################
- # File: tidycovid-logratios.R
- #
- # based on https://robjhyndman.com/hyndsight/logratios-covid19/
- ##############################################################################
- # install.packages("devtools") # need this for install_github
- library(devtools)
- # install.packages("tidyverse")
- library(tidyverse)
- # install.packages("tsibble")
- library(tsibble)
- # See https://www.r-bloggers.com/meet-tidycovid19-yet-another-covid-19-related-r-package/
- # install_github("joachim-gassen/tidycovid19")
- library(tidycovid19)
- #install.packages("ggthemes")
- library(ggthemes)
- # Download latest data
- updates <- download_merged_data(cached = TRUE)
- # Countries to highlight
- countries <- c("SGP", "CHN", "USA", "KOR", "TWN", "JPN")
- start_from_date <- as.Date("2020-02-01")
- #
- # Plot 1 - Confirmed cases (log scale) Y vs. Days after 100th confirmed case
- #
- updates %>%
- plot_covid19_spread(
- highlight = countries,
- type = "confirmed",
- edate_cutoff = 40
- )
- #
- # Plot 2 - log ratios and plot them for each country
- # with a smooth curve overlaid.
- #
- updates %>%
- mutate(cases_logratio = difference(log(confirmed))) %>%
- filter(
- iso3c %in% countries,
- date >= start_from_date
- ) %>%
- ggplot(aes(x = date, y = cases_logratio, col = country)) +
- geom_point() +
- geom_smooth(method = "loess") +
- facet_wrap(. ~ country, ncol = 3) +
- xlab("Date") +
- ggthemes::scale_color_colorblind()
- #
- # Plot 3 - Show only the smoothed log ratio
- # (not the standard error band around it as in plot #2)
- #
- updates %>%
- mutate(
- cases_logratio = difference(log(confirmed))
- ) %>%
- filter(iso3c %in% countries) %>%
- filter(date >= start_from_date) %>%
- ggplot(aes(x = date, y = cases_logratio, col = country)) +
- geom_hline(yintercept = log(2)/c(2:7,14,21), col='grey') +
- geom_smooth(method = "loess", se = FALSE) +
- scale_y_continuous(
- "Daily increase in cumulative cases",
- breaks = log(1+seq(0,60,by=10)/100),
- labels = paste0(seq(0,60,by=10),"%"),
- minor_breaks=NULL
- #,
- #sec.axis = sec_axis(~ log(2)/(.),
- # breaks = c(2:7,14,21),
- # name = "Doubling time (days)")
- ) +
- ggthemes::scale_color_colorblind()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement