Analyzing Covid-19 Public Health Data with R
By: Nash Ali
Tags: Ggplot2, Rstudio, Covid 19, Data Science, Data Visualization

Data Analysis has changed the world as we see it. With the ongoing COVID-19 pandemic, it is extremely important to look at the public health data and learn how the world is doing according to the numbers.

For this article, we will analyze public health data on Covid-19 to learn and visualize the death rate amongst various countries. The data will be irrelevant in the future as confirmed cases and deaths continue to rise. However, this will be a great practice for learners.

Warning: The content of this post may seem very elementary to professionals. But the R code is polished and short. You must know the basics of R to understand it and work with it.

Resources

To learn or refresh your knowledge of R, DataCamp is extremely helpful. You just need to create a free account.

The free online textbook Introduction to Data Analysis and Prediction Algorithms with R is also really helpful.

R tips

Use Microsoft R Open if possible. It comes with the multi-threaded math library (MKL) and checkpoint package.

But don’t hesitate to switch to regular R if you run into trouble. Microsoft R Open can have some bizarre problems in my personal experiences. Don’t waste too much time on fixing them. Switching to regular R often solves the problem.

Install regular R via CRAN (Instructions for Ubuntu). Install checkpointto ensure reproducibility (it is not a Microsoft R Open exclusive.)

Use RStudio and make good use of its Console window. Some people hold strong feelings against R because of some of its uniqueness compared to other major programming languages. In fact, a lot of the confusion can be resolved with simple queries in the Console. Not sure whether the vector index starts from zero or one? c(1,2,3)[1] tells you it’s one. Querying 1:10 tells you the result includes 10 (unlike Python).

Accessing the Data

The data gathered on April 26th, 2020 is from WorldOMeter and is compiled in an Excel file. Please, click here for its link to Github. The Excel file was imported into a Data Frame on RStuido.

Data Visualization

At first, we load the libraries we need to access and the functions we want to utilize. These are some of the most powerful packages in R. Please, click here to read about these at your convenience.

# load libraries

library(tidyverse)

library(ggrepel)

library(ggthemes)

library(dslabs)

Now, we define the point of intercept in our plot which will connect all the death rates of each country in the plot. We define an object r where deaths in each country are divided by the respective country’s population and multiplied by 1,000,000 to calculate the death rates in each country as per 1 million.

# define the intercept

r <- nations %>%

summarize(rate = sum(deaths) / sum(population) * 10^6) %>%

.$rate

Now, we make our plot. We use ggplot function to plot the population (per 1 million) against the deaths.

# make the plot, combining all elements

nations %>%

ggplot(aes(population/10^6, deaths,

label = country)) # label the plots with countries +



# We will use log scale in our plot, so we log scale the intercept

geom_abline(intercept = log10(r), lty = 1, color = "red") + # Categorize the colors of the labels in continents

geom_point(aes(col = continent), size = 2) +

geom_text_repel() +



# Scale the X-axis and Y-axis

scale_x_log10() +

scale_y_log10() + xlab("Population in millions (log scale)") +

ylab("Total number of deaths (log scale)") +

ggtitle("The Global Covid-19 Deaths") +

scale_color_discrete(name = "Legend") +

theme_economist() # we use the economist theme

It seems that the U.S tends to have the highest death rate followed by Italy, France, Spain, and the U.K. But please note that population and social distancing play a key role in infection rates. The countries with the highest death rates are highly populated.

The end

Thanks for reading! Please, stay safe.