Plotting Covid-19 Pandemic

Interactive plot of Covid-19 cases

Ashwin Malshe www.ashwinmalshe.com
03-08-2020

In this post, we will visualize spread of worldwide COVID-19 cases through time. I obtained the data from Rami Krispin’s website: https://ramikrispin.github.io/coronavirus/ using coronovirus package. I also decided to do some experimentation using John Coene’s fantastic echarts4r package, which allows us to access echarts API.

Load the libraries and get the data in the R session.

library(dplyr)
library(echarts4r)
library(coronavirus)

# Get the data
data("coronavirus")

Data Preparation

Print out the first 6 observations.

head(coronavirus)
        date province     country      lat     long      type cases
1 2020-01-22          Afghanistan 33.93911 67.70995 confirmed     0
2 2020-01-23          Afghanistan 33.93911 67.70995 confirmed     0
3 2020-01-24          Afghanistan 33.93911 67.70995 confirmed     0
4 2020-01-25          Afghanistan 33.93911 67.70995 confirmed     0
5 2020-01-26          Afghanistan 33.93911 67.70995 confirmed     0
6 2020-01-27          Afghanistan 33.93911 67.70995 confirmed     0

We are interested in date and type. Let’s take a look at the distinct values for type.

coronavirus %>% count(type)
       type     n
1 confirmed 75576
2     death 75576
3 recovered 71910

There are only 3 values: confirmed, death, and recovered. Next we will create sum of cases for each of the values and store them in separate data sets.

dt1 <- coronavirus %>% 
  filter(type == "confirmed") %>% 
  group_by(date) %>% 
  summarize(Confirmed = sum(cases, na.rm = TRUE), .groups = "drop")

dt2 <- coronavirus %>% 
  filter(type == "death") %>% 
  group_by(date) %>% 
  summarize(Death = sum(cases, na.rm = TRUE), .groups = "drop")


dt3 <- coronavirus %>% 
  filter(type == "recovered") %>% 
  group_by(date) %>% 
  summarize(Recovered = sum(cases, na.rm = TRUE), .groups = "drop")

Finally, we will merge the 3 datasets so that we will have the counts of each type in separate columns.

dt <- dt1 %>% 
  inner_join(dt2, by = "date") %>% 
  inner_join(dt3, by = "date")

Plot

Finally, time to make the plot! Note how we can build this plot in separate elements.

dt %>% 
  e_charts(x = date) %>% 
  e_line(serie = Confirmed) %>% 
  e_line(serie = Death) %>% 
  e_line(serie = Recovered) %>% 
  e_tooltip(trigger = "axis") %>% 
  e_datazoom(type = "slider") %>% 
  e_title("Worldwide COVID-19 cases") %>% 
  e_theme("bee-insipired") 


This plot is interactive so you can hover over the plot to get the exact readings. You can also toggle time series on or off by clicking on the legends on top.