The COVID-19 dataset is a dataset with daily level information on the number of COVID-19 infections, deaths and recovery. This timeseries dataset covers data from January 2020 - May 2021.
To start, let us read in the data set and ensure that all columns are read in with the appropriate column types.
covid19_df <- read_csv(here::here('data/covid_19_data.csv')) %>%
mutate(ObservationDate = as.Date(ObservationDate, format="%m/%d/%Y"),
`Last Update` = as.POSIXct(`Last Update`, format = "%m/%d/%Y %H:%M")
)
In initial peak at the dataset shows that there are
8 fields in this dataset. Each row/record has a serial
number that acts as a record identifier, an observation date,
Province/State, Country/Region, the record’s last updated time, and the
cumulative number of confirmed infections, deaths and recoveries.
head(covid19_df)
## # A tibble: 6 × 8
## SNo ObservationDate `Province/State` `Country/Region` `Last Update`
## <dbl> <date> <chr> <chr> <dttm>
## 1 1 2020-01-22 Anhui Mainland China 2020-01-22 17:00:00
## 2 2 2020-01-22 Beijing Mainland China 2020-01-22 17:00:00
## 3 3 2020-01-22 Chongqing Mainland China 2020-01-22 17:00:00
## 4 4 2020-01-22 Fujian Mainland China 2020-01-22 17:00:00
## 5 5 2020-01-22 Gansu Mainland China 2020-01-22 17:00:00
## 6 6 2020-01-22 Guangdong Mainland China 2020-01-22 17:00:00
## # ℹ 3 more variables: Confirmed <dbl>, Deaths <dbl>, Recovered <dbl>
Taking a closer look at the column values, we can see that there are
228 unique countries and
737 unique Provinces/States in this dataset.
knitr::kable(covid19_df %>%
summarise_all(n_distinct) %>%
pivot_longer(everything())
)
| name | value |
|---|---|
| SNo | 306429 |
| ObservationDate | 494 |
| Province/State | 737 |
| Country/Region | 228 |
| Last Update | 44 |
| Confirmed | 107146 |
| Deaths | 20089 |
| Recovered | 74541 |
The negative values in the Confirmed,
Deaths, Recovered columns are most likely bad
data points and will need to be cleaned out for analysis.
knitr::kable(
covid19_df %>%
select(where(is.numeric)) %>%
summarise(across(everything(),
list(N=n_distinct, Min=min, Mean=mean, Median=median, Max=max), na.rm = TRUE)
) %>%
pivot_longer(cols = everything(),
names_to = c('col', '.value'),
names_sep = '_')
)
| col | N | Min | Mean | Median | Max |
|---|---|---|---|---|---|
| SNo | 306429 | 1 | 153215.000 | 153215 | 306429 |
| Confirmed | 107146 | -302844 | 85670.912 | 10375 | 5863138 |
| Deaths | 20089 | -178 | 2036.403 | 192 | 112385 |
| Recovered | 74541 | -854405 | 50420.286 | 1751 | 6399531 |
And finally, there are also some implausible dates in the
Last Update column (e.g. 0020-01-23 17:00:00) that will
require data clean up prior to analysis.
knitr::kable(
covid19_df %>%
select(where(is.Date) | where(is.timepoint)
) %>%
summarise(across(everything(),
list(N=n_distinct, Min=min, Mean=mean, Median=median, Max=max), na.rm = TRUE)
) %>%
pivot_longer(cols = everything(),
names_to = c('col', '.value'),
names_sep = '_')
)
| col | N | Min | Mean | Median | Max |
|---|---|---|---|---|---|
| ObservationDate | 494 | 2020-01-22 00:00:00 | 2020-11-06 00:00:00 | 2020-11-10 00:00:00 | 2021-05-29 00:00:00 |
| Last Update | 43 | 0020-01-23 17:00:00 | 0143-10-20 22:36:31 | 0020-03-08 05:31:00 | 2020-02-01 19:53:00 |
Check for records where the number of recovered or death N is greater than the confirmed N. These are also likely bad data points that will need to be omitted for analysis.
knitr::kable(
covid19_df %>%
filter(`Recovered` > `Confirmed`,
`Deaths` > `Confirmed`
) %>%
select(SNo, `Confirmed`, `Recovered`, `Deaths`)
)
| SNo | Confirmed | Recovered | Deaths |
|---|---|---|---|
| 34249 | 0 | 21325 | 1188 |
| 34917 | 0 | 21605 | 1275 |
| 129569 | 0 | 2889 | 164 |
| 131063 | 0 | 6656 | 174 |
| 134803 | 0 | 10170 | 159 |
| 144529 | 0 | 8474 | 173 |
| 147525 | -302844 | 0 | 0 |
| 155789 | 0 | 7650 | 178 |
| 156551 | 0 | 8236 | 160 |
First, let’s see the Top 50 countries by total number of Confirmed cases in this dataset.
# Bar chat with the Top 10 countries by confirmed cases over time
fig <- plot_ly(top_confirmed_by_country_df,
x = ~`Country/Region`,
y = ~total_confirmed_by_country,
type = 'bar',
name = 'Confirmed')
fig <- fig %>% add_trace(y = ~total_deaths_by_country, name = 'Deaths')
fig <- fig %>% layout(yaxis = list(title = 'Count'), barmode = 'group',
xaxis = list(title = 'Country/Region', categoryorder = "total descending"))
fig
Next, zooming in on the US portion of the data, here is a pie chart showing each state’s contribution to the confirmed new cases in this dataset. To make visualization easier, the states contributing <10% of the cases have been grouped as “Others”.
fig2 <- plot_ly(total_cases_us, labels = ~state_grouped,
values = ~total_confirmed_by_state_grouped,
type = 'pie')
fig2 <- fig2 %>% layout(title = 'Total number of confirmed COVID-19 cases by State',
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
fig2
Finally, let’s examine the confirmed cases over time in
Mainland China by Providence.
fig3 <- plot_ly(china_df, x = ~ObservationDate,
y = ~confirmed_cleaned,
type = 'scatter',
color=~`Province/State`,
mode = 'lines')
fig3
The Providence of Hubei is a clear outlier so let’s plot the graph again by excluding that Providence.
fig4 <- plot_ly(china_df %>%
filter(`Province/State` != 'Hubei'), x = ~ObservationDate,
y = ~confirmed_cleaned,
type = 'scatter',
color=~`Province/State`,
mode = 'lines')
fig4
Code used to generate this document can be found here