Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
1,310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 2.30 KB | None | 0 0
  1. ##############################################################################
  2. # File: tidycovid-logratios.R
  3. #
  4. # based on https://robjhyndman.com/hyndsight/logratios-covid19/
  5. ##############################################################################
  6.  
  7.  
  8. # install.packages("devtools") # need this for install_github
  9. library(devtools)
  10.  
  11. # install.packages("tidyverse")
  12. library(tidyverse)
  13.  
  14. # install.packages("tsibble")
  15. library(tsibble)
  16.  
  17. # See https://www.r-bloggers.com/meet-tidycovid19-yet-another-covid-19-related-r-package/
  18.  
  19. # install_github("joachim-gassen/tidycovid19")
  20. library(tidycovid19)
  21.  
  22. #install.packages("ggthemes")
  23. library(ggthemes)
  24.  
  25.  
  26.  
  27. # Download latest data
  28. updates <- download_merged_data(cached = TRUE)
  29.  
  30. # Countries to highlight
  31. countries <- c("SGP", "CHN", "USA", "KOR", "TWN", "JPN")
  32.  
  33. start_from_date <- as.Date("2020-02-01")
  34.  
  35. #
  36. # Plot 1 - Confirmed cases (log scale) Y vs. Days after 100th confirmed case
  37. #
  38. updates %>%
  39.   plot_covid19_spread(
  40.     highlight = countries,
  41.     type = "confirmed",
  42.     edate_cutoff = 40
  43.   )
  44.  
  45. #
  46. # Plot 2 -  log ratios and plot them for each country
  47. # with a smooth curve overlaid.
  48. #
  49. updates %>%
  50.   mutate(cases_logratio = difference(log(confirmed))) %>%
  51.   filter(
  52.     iso3c %in% countries,
  53.     date >= start_from_date
  54.   ) %>%
  55.   ggplot(aes(x = date, y = cases_logratio, col = country)) +
  56.   geom_point() +
  57.   geom_smooth(method = "loess") +
  58.   facet_wrap(. ~ country, ncol = 3) +
  59.   xlab("Date") +
  60.   ggthemes::scale_color_colorblind()
  61.  
  62.  
  63. #
  64. # Plot 3 - Show only the smoothed log ratio
  65. # (not the standard error band around it as in plot #2)
  66. #
  67. updates %>%
  68.   mutate(
  69.     cases_logratio = difference(log(confirmed))
  70.   ) %>%
  71.   filter(iso3c %in% countries) %>%
  72.   filter(date >= start_from_date) %>%
  73.   ggplot(aes(x = date, y = cases_logratio, col = country)) +
  74.   geom_hline(yintercept = log(2)/c(2:7,14,21), col='grey') +
  75.   geom_smooth(method = "loess", se = FALSE) +
  76.   scale_y_continuous(
  77.     "Daily increase in cumulative cases",
  78.     breaks = log(1+seq(0,60,by=10)/100),
  79.     labels = paste0(seq(0,60,by=10),"%"),
  80.     minor_breaks=NULL
  81.     #,
  82.     #sec.axis = sec_axis(~ log(2)/(.),
  83.     #                    breaks = c(2:7,14,21),
  84.     #                    name = "Doubling time (days)")
  85.   ) +
  86.   ggthemes::scale_color_colorblind()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement