Photo by CDC on Unsplash

Plotting COVID-19 Epidemic

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

Check out the first 6 observations.

head(coronavirus)
## # A tibble: 6 x 7
##   Province.State Country.Region   Lat  Long date       cases type     
##   <chr>          <chr>          <dbl> <dbl> <date>     <int> <chr>    
## 1 ""             Japan           36    138  2020-01-22     2 confirmed
## 2 ""             South Korea     36    128  2020-01-22     1 confirmed
## 3 ""             Thailand        15    101  2020-01-22     2 confirmed
## 4 "Anhui"        Mainland China  31.8  117. 2020-01-22     1 confirmed
## 5 "Beijing"      Mainland China  40.2  116. 2020-01-22    14 confirmed
## 6 "Chongqing"    Mainland China  30.1  108. 2020-01-22     6 confirmed

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

coronavirus %>% count(type)
## # A tibble: 3 x 2
##   type          n
##   <chr>     <int>
## 1 confirmed  1777
## 2 death       230
## 3 recovered  1145

There are only 3 values: confirmed, death, and recovered. Next we will create frequency counts for each of the values and sotre them in separate datasets.

dt1 <- coronavirus %>% 
  filter(type == "confirmed") %>% 
  count(date, name = "Confirmed")

dt2 <- coronavirus %>% 
  filter(type == "death") %>% 
  count(date, name = "Death")

dt3 <- coronavirus %>% 
  filter(type == "recovered") %>% 
  count(date, name = "Recovered")

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

dt %>% 
  e_charts(x = date) %>% 
  e_line(serie = Confirmed) %>% 
  e_line(serie = Death) %>% 
  e_line(serie = Recovered) %>% 
  e_tooltip(trigger = "axis") %>% 
  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.

Related

comments powered by Disqus