The dataset used in this dataset has been extracted from ourworldindata.org

library(ggplot2)
library(dplyr)
library(tidyr)
library(gganimate)
library(lubridate)
library(gifski)
library(av)
library(countrycode)

We will convert the date into standard format for handling it easily.

 daily_cases <-daily_cases %>%
  mutate(Date = mdy(`Date`))

colnames(daily_cases)[4] <- "Daily_confirmed_cases"
head(daily_cases)
##        Entity Code       Date Daily_confirmed_cases
## 1 Afghanistan  AFG 2019-12-31                     0
## 2 Afghanistan  AFG 2020-01-01                     0
## 3 Afghanistan  AFG 2020-01-02                     0
## 4 Afghanistan  AFG 2020-01-03                     0
## 5 Afghanistan  AFG 2020-01-04                     0
## 6 Afghanistan  AFG 2020-01-05                     0

Now, lets create a time series animated plot by counting how many cases are there by date.

daily_cases %>%
  group_by(Date) %>%
  mutate(Cumulative = cumsum(Daily_confirmed_cases))%>%
  ggplot(aes(x = Date, y = Cumulative))+
  geom_line(col= "red")+
  geom_point(size =2)+
  theme_light()+
  ggtitle("Daily cumulative cases")+
  transition_reveal(Cumulative)

We have created a column of cumulative cases everyday. From the plot we observe that the cases have increased exponentially March end onward.

daily_cases$Day<- day(daily_cases$Date)
daily_cases$Month<- month(daily_cases$Date)
head(daily_cases)
##        Entity Code       Date Daily_confirmed_cases Day Month
## 1 Afghanistan  AFG 2019-12-31                     0  31    12
## 2 Afghanistan  AFG 2020-01-01                     0   1     1
## 3 Afghanistan  AFG 2020-01-02                     0   2     1
## 4 Afghanistan  AFG 2020-01-03                     0   3     1
## 5 Afghanistan  AFG 2020-01-04                     0   4     1
## 6 Afghanistan  AFG 2020-01-05                     0   5     1

Animated line plot

lp <- daily_cases%>%
  filter(Month == 3)%>%
  group_by(Day,Entity)%>%
  ungroup()%>%
  mutate(Cumulative = cumsum(Daily_confirmed_cases))

Filter the data to include countries.

lp%>%filter(Entity %in% c("United States", "France", "United Kingdom","Germany"))%>%
  ggplot(aes(Day, y = Daily_confirmed_cases, group = Entity, color = Entity))+
  geom_line()+ geom_point()+theme_bw()+
  ggtitle("Animated Daily Line Plot")+ 
  transition_reveal(Day)

We see a sharp jump after the 15th of March

Animated Bar Plots Preparing data for Bar Plot

bp <- daily_cases %>%
  filter(Entity %in% c("United States", "France", "United Kingdom","Germany")) %>%
  filter(Month %in% c(3,4,5))%>%
  group_by(Entity, Month)
bar_plot <-bp %>%
  ggplot(aes(x = Entity, y = Daily_confirmed_cases, fill = Entity))+
  geom_bar(stat = "identity")+
  theme_bw()

Animated Bar plot by month

Animated Bar plot by Country We see a huge jump in cases in United Kingdom.

Animated Bubble plot

Let’s read a new data set that also contains the daily death rate per country.

total_data <-total_data %>%
  mutate(Date = mdy(`Date`))

colnames(total_data)[4] <- "Daily_confirmed_cases"
colnames(total_data)[5] <- "Daily_confirmed_deaths"
total_data$Month1 <- as.integer(month(total_data$Date))
head(total_data)
##        Entity Code       Date Daily_confirmed_cases Daily_confirmed_deaths
## 1 Afghanistan  AFG 2019-12-31                     0                      0
## 2 Afghanistan  AFG 2020-01-01                     0                      0
## 3 Afghanistan  AFG 2020-01-02                     0                      0
## 4 Afghanistan  AFG 2020-01-03                     0                      0
## 5 Afghanistan  AFG 2020-01-04                     0                      0
## 6 Afghanistan  AFG 2020-01-05                     0                      0
##   Month1
## 1     12
## 2      1
## 3      1
## 4      1
## 5      1
## 6      1
bubble <- total_data %>%
  filter(Entity %in% c("United States", "France", "United Kingdom","Germany")) %>%
  filter(Month1 %in% c(1,2,3,4,5,6))%>%
  group_by(Entity, Month1)
bubble_plot <- ggplot(bubble, aes(x = Daily_confirmed_cases, y = Daily_confirmed_deaths, size = Daily_confirmed_cases/Daily_confirmed_deaths, colour = Entity))+
  geom_point(show.legend = F,alpha = 0.6)+
  labs("Daily Cases", "Daily Deaths")+
  scale_size(range=c(2,5))

Adding the animated layers now.