COVID-19 visualizations with R and Tableau

Harish Nagpal
Analytics Vidhya
Published in
8 min readMay 17, 2020

--

The world was a happy place till 1st March 2020 and then an invisible enemy flew from China and it devastated the whole world.

When J. Robert Oppenheimer, often called the “father of the atomic bomb, was selected to administer a laboratory to carry out the Manhattan Project, the program that developed the first nuclear weapon during World War II, little did he might have realized that world would not need an atomic weapon to devastate mankind after almost 81 years. A single virus is enough to destroy whole mankind.

Photo by Tobias Rehbein on Unsplash

Never in the world, any one,even in his dreams, would have thought that, an invisible enemy, originated from China in December, 2019 would unleash its terror in such a way that people around the world won’t be able to step out of their homes. Covid-19 has devastated the economies of all countries for the time being. In many countries scores of people have become unemployed. Few African countries are facing shortage of food and water.

It seems that the Earth has stopped its movement on its axis.

Covid-19 has done an irreversible damage to the world economies and human race. Even if scientists develops the vaccine to remove it from the world in near future, definitely the world would not be same again.

In this article I want to show you some visualizations which you can do in Tableau. I have cleaned the data in R and using R, I prepared a csv file which I used in Tableau to generate visualizations.

R recently developed a library covid19.analytics. Whenever you use it in your code, it will fetch the latest data for you. It’s a beautiful package where you can do some beautiful visualizations. Since it is still being developed, I used it to generate data for myself and used this data in Tableau visualizations.

I could have used R ggplot2 also to do the visualizations. But I find that Tableau gives you more space to think out of the box.

Since I wrote the article on 17/05/2020, the data is up to 16/05/2020 only.

Here is the R code for you, which you can directly use. You just need to copy and paste the code in your project.

Once you run few lines of codes, you will find the date up to which data is present.

> x <- covid19.data()
Data being read from JHU/CCSE repository
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Reading data from https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/05-16-2020.csv

R code

library(covid19.analytics)
library(dplyr)
library(tidyr)
covid19.data(case = “aggregated”, local.data = FALSE, debrief = FALSE)
x <- covid19.data()names(x)# Check death, confirmed and recovered cases
covid19.TS.deaths <- covid19.data(“ts-deaths”)
covid19.TS.confirmed <- covid19.data(“ts-confirmed”)
covid19.TS.recovered <- covid19.data(“ts-recovered”)
#write.csv(covid19.TS.deaths, file = ‘D:/Harish/R practice projects/Corona/Covid R Package/Deaths.csv’, row.names = F)#covert dates to column ‘Date’ with gather function and no of cases to Deaths, Confirmed and Recovered columns#5:XXX value denotes date column. Add one day for each day.finalDeath <- gather(covid19.TS.deaths, “Date”, “Deaths”, 5:120)
finalConfirmed <- gather(covid19.TS.confirmed, “Date”, “Confirmed”, 5:120)
finalRecovered <- gather(covid19.TS.recovered, “Date”, “Recovered”, 5:120)
#write.csv(finalDeath, file = ‘D:/Harish/R practice projects/Corona/Covid R Package/finalDeath.csv’, row.names = F)#grouping on country and date — Deaths, Confirmed and Recovered cases
finalDeath1 <- finalDeath %>% select(Country.Region, Date, Deaths) %>%
group_by(Country.Region, Date) %>% summarise(deaths = sum(Deaths))
nrow(finalDeath1)
finalConfirmed1 <- finalConfirmed %>% select(Country.Region, Date, Confirmed) %>%
group_by(Country.Region, Date) %>% summarise(confirmed = sum(Confirmed))
nrow(finalConfirmed1)
finalRecovered1 <- finalRecovered %>% select(Country.Region, Date, Recovered) %>%
group_by(Country.Region, Date) %>% summarise(recovered = sum(Recovered))
nrow(finalRecovered1)
#Changing name of Country.Region to Country
names(finalConfirmed1)[names(finalConfirmed1) == “Country.Region”] <- “Country”
names(finalDeath1)[names(finalDeath1) == “Country.Region”] <- “Country”
names(finalRecovered1)[names(finalRecovered1) == “Country.Region”] <- “Country”
#Merging three datasets to make a single DatasetCovidData <- merge(finalConfirmed1, finalDeath1, by = c(“Country”,”Date”))FinalCovidData <- merge(CovidData, finalRecovered1, by = c(“Country”,”Date”))#making a new column Active
FinalCovidData$Active <- (FinalCovidData$confirmed — FinalCovidData$deaths — FinalCovidData$recovered)
write.csv(FinalCovidData, file = ‘D:/Harish/R practice projects/Corona/Covid R Package/FinalCovidData.csv’, row.names = F)## code ends here for Tableau preparation data

One thing you need to make sure that, in below line of code you need to take care of the last date. You need to increment 120 by 1 for each day. For example if data is up to 17th May then you need to use 5:121 and so on.

finalDeath <- gather(covid19.TS.deaths, “Date”, “Deaths”, 5:120)

I will use “FinalCovidData.csv” in my Tableau visualization. I changed the file name to “owid-covid-data.csv”. I have imported this file in Tableau window. This file contains confirmed, deaths, recovered and Active cases, country wise and date wise.

The file contains below fields:

Country, Date, confirmed, deaths, recovered and Active

I created two calculated fields > Confirmed and Ranking also in Tableau.

See the file structure as given below in Tableau data source window.

Tableau data source screen

Let us make Racing Circles chart from above data.

I have plotted Confirmed cased on X-axis and Death cases on Y-Axis. Since I want to show it as an animated chart, I used Format -> Animations option to show this page as animated page which animates basing upon date field.

Follow below steps to create the chart:

  1. Drag confirmed field to Columns shelf and deaths field to Rows shelf from Measures.
  2. Drag Date field to Pages to animate the data on date. Once you set this option, you will see a video like start, stop buttons window in the screen.
  3. Drag Country and Date to Filters.
  4. Drag Country to Colour and Label Marks.
  5. Drag confirmed to Size Marks.
  6. Select Circle from Marks drop down window.
  7. Format the axis as per your liking
  8. You will see how the chart has shaped.
  9. Set animation speed the way you want to show the data. See the parameters I have set below in Animations window.
Animation options

To put date field on the chart, Right click on the chart and click Annotate > Point and put its values as given below.

Once you set all above options, your final screen will be like as given below:

Racing circles chart final screen

Click on Play button and you will see circles start emerging. The size of the circles increases as confirmed and death cases increase country wise.

This is the final output as given below. Click below screen and then click on different buttons to enlarge it.

Racing circles chart for confirmed and deaths cases

Line Chart

Now let us make a simple line chart of active, confirmed, death and recovered cases.

Drag Date field to Columns shelf and confirmed field to Rows shelf. Here is a catch. Drag Active, deaths and recovered fields to Y-axis and not on shelves. So you will get all numeric values on Y-axis in the form of Measure Values. Drag Measure Values to Colour Marks. Change Marks to Line. And you will get a line chart. See the below screen shot for reference.

Line chart

World Map

Let us draw a world map chart for confirmed cases.

Drag Longitude to Columns shelf and Latitude to Rows shelf. We already have country names in Country field. Drag Country to Colour and Details Marks. You will soon see a world map taking shape. Tableau will automatically plot world map for you.

Create a new calculated field Confirmed and set it value as given below and drag this field to Size and Label Marks. Change Marks to Circle in drop down window.

Confirmed calculate field.

Increase or decrease circle size by adjusting circle size in Size Marks. I want a black map so I set the map value as Map > Background Maps > Dark.

Once you set all the values correctly, your screen will look like below screen:

World map screen

Maximize the screen and your final screen will be like this as given below:

World Map final screen

Racing bars chart

You must have got this chart from your friends in whatsup videos. So let’s create this chart.

You must have noticed that the chart automatically sorts the countries basing upon confirmed cases that is, it is ranking the countries as date changes. It basically uses Animation feature of Tableau.

We will use same Animation feature as we used in Racing circles chart.

Create a new calculated field Ranking and put values as given below:

Ranking calculated field
  1. Drag confirmed field to Columns Shelf and Ranking field to Rows Shelf.
  2. Change Marks to Bar in the drop down.
  3. Drag Country to Colour, Tooltip and Label Marks.
  4. Drag confirmed to Label and set it value as Max.
  5. Drag Date to Pages. You will see animation buttons, with a date value.
  6. To show values of country and cases on bars itself, set their alignment value in Label Marks window.
  7. You will see a chart as given below in your screen. Try to press Play button. You will see bars moving up and down as their value increase or decrease. Click on below screen to enlarge it.
Racing bars final screen

To put date field on the chart, Right click on the chart and click Annotate > Point and put its values as given below:

Annotation for Date

Drag the Annotation anywhere on the screen and put it where you want it to be. The final screen will look given below:

Racing bars screen after adding Date field on screen

Once everything is set, maximize the screen and drag the date to first date and press the Play button. You will see the bars moving up and down as given below. Click on the chart to enlarge it.

Racing bars chart

Hope you liked my article. See you soon in next article.

See my data science maths art here

Connect with me on Linkedln here.

--

--

Harish Nagpal
Analytics Vidhya

An IT professional, passionate about art in all forms — data, nature, paintings and visual art. Linkedln — https://www.linkedin.com/in/harish-nagpal-8696529/