COVID-19: WHO Status Reports
Introduction
This document records my efforts to work out displays for COVID-19 cases, based on status reports from the World Health Organization, as recorded in Edward Brown’s package data2019nCoV.
Installation and Setup
General tools we use:
Package data2019nCoV updates frequently, so you’ll want to check the Github repository for updates and re-install as needed:
The Data
We’ll work with WHO_SR:
Each row is a WHO situation report for a large number of countries (and some other groups such as health care providers).
Comparing a Few Countries
Our goal is to write functions that permit the user to view the number of confirmed cases over time, for selected countries.
Basic selection:
select_who <- function(countries) {
## makes a regex like "Iran|France|Spain":
regex <- paste0(countries, collapse = "|")
WHO_SR %>%
## selec tonly columns contaning name of
## one of the desired countries:
select(Date, matches(regex)) %>%
## just looking for total confirmed cases for each country
## not breakdown by region, etc:
select(-matches("\\."))
}Try it out:
countries <- c("UnitedStatesofAmerica", "Canada", "Brazil")
df <-
countries %>%
select_who()
DT::datatable(df)Each row is a small part of a WHO status report.
We will transform the data so that it is glyph-ready for a plot of the number of confirmed cases in each country against the date of the report. tidyr::gather() is our friend, here.
Note: It is very handy that gather() allows one to specify the columns to be gathered via a character-vector of the column names.
df_long %>%
ggplot(aes(x = Date, y = value, col = key)) +
geom_line() +
labs(
title = "Confirmed Cases",
x = "Date",
y = "Confirmed Cases"
) +
theme(legend.title = element_blank())Very nice. But can we make the graph interactive?
## make plot object
p <-
df_long %>%
ggplot(aes(x = Date, y = value, col = key,
text = glue::glue(
"{key}<br>{value}<br>{Date}"
))) +
geom_line() +
labs(
title = "Confirmed Cases",
x = "Date",
y = "Confirmed Cases"
) +
theme(legend.title = element_blank())
## call in plotly:
plotly::ggplotly(p, tooltip = "text") %>%
plotly::config(displayModeBar = FALSE)Oops! The tooltip shows up fine, but the line plots are missing. I have googled this problem and found that (for reasons I don’t yet understand), you can get the lines if you use the group aesthetic, as follows:
## make plot object mostly as before:
p <-
df_long %>%
ggplot(aes(x = Date, y = value, col = key,
group = key, ## to satisfy ggplotly!!
text = glue::glue(
"{key}<br>{value}<br>{Date}"
))) +
geom_line() +
labs(
title = "Confirmed Cases",
x = "Date",
y = "Confirmed Cases"
) +
theme(legend.title = element_blank())
## call in plotly:
plotly::ggplotly(p, tooltip = "text") %>%
plotly::config(displayModeBar = FALSE)Encapsulate in a Function
We can encapsulate our programming work into a function:
cases_plot <- function(countries) {
p <-
select_who(countries) %>%
gather(key, value, countries) %>%
ggplot(aes(x = Date,
y = value,
col = key,
group = key,
text = glue::glue(
'{key}<br>{value}<br>{Date}'
))) +
geom_line() +
theme(legend.position="none") +
labs(title = "Confirmed Cases",
x = "Date",
y = "Confirmed Cases") +
theme(legend.title = element_blank())
ggplotly(p, tooltip = "text") %>% config(displayModeBar = FALSE)
}Try it out:
This works, but note that for some reason we lost the legend! Thankfully, the presence of the tooltip prevents this from being a big deal.
Where to Go From Here
- Develop other analyses, e.g.:
- display the data on a world map;
- dig down into suspected cases, deaths, etc., to the extent provided by
WHO_SR; - etc.
- Shiny-fy our analysis.
- Continue o study of
plotly::ggplotly(), along with other interactive-graphics tools.
For more ideas consult the package vignettes: