-
Notifications
You must be signed in to change notification settings - Fork 0
/
MATH0216_A7.Rmd
117 lines (90 loc) · 3.19 KB
/
MATH0216_A7.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
---
title: "Assignment 7"
author: "Nick Bermingham"
date: "5/1/2020"
output: html_document
---
For the last assignment of MATH 216, I decided to visualize COVID-19 data. I used a dataset from healthdata.gov, which had the number of total deaths in each country every day from late January to the end of April. Focusing on countries in the European Union, I have made two plots - one with an animated line graph over time, and another with a map of Europe that animates over time.
```{r message=FALSE, warning=FALSE, include=FALSE}
library(tidyverse)
library(gganimate)
library(ggplot2)
library(plyr)
library(gifski)
c_data <- read_csv('time_series_covid19_deaths_global.csv')[,-c(1,3,4)]
colnames(c_data)[1] <- "country"
c_data <- ddply(c_data, "country", numcolwise(sum))
head(c_data)
```
```{r include=FALSE, warning=FALSE}
eu_countries <- c("Austria",
"Belgium",
"Bulgaria",
"Croatia",
"Cyprus",
"Czech Republic",
"Denmark",
"Estonia",
"Finland",
"France",
"Germany",
"Greece",
"Hungary",
"Ireland",
"Italy",
"Latvia",
"Lithuania",
"Luxembourg",
"Malta",
"Netherlands",
"Poland",
"Portugal",
"Romania",
"Slovakia",
"Slovenia",
"Spain",
"Sweden")
c_data_2 <- c_data %>%
filter(country %in% eu_countries) %>%
gather(key = Date, value = Cases, 2:101) %>%
arrange(country)
c_data_2$Date <- as.Date(c_data_2$Date, "%m/%d/%Y")
```
```{r include=FALSE, warning=FALSE}
line_animation <- c_data_2 %>%
ggplot(aes(x = Date, y = Cases, group = country, color = factor(country))) +
geom_line()+
scale_color_discrete()+
labs(x = "Time", y = "Deaths", title = "Number of COVID-19 Deaths", color = "Country") +
transition_reveal(Date)
```
```{r echo=FALSE, warning=FALSE}
animate(line_animation, duration = 10, fps = 20, renderer = gifski_renderer())
```
```{r include=FALSE, warning=FALSE}
library(rnaturalearth)
library(rnaturalearthdata)
world <- ne_countries(scale = 'medium', type = 'map_units',returnclass = 'sf')
europe <- world %>%
filter(name %in% eu_countries) %>%
arrange(name)
```
```{r include=FALSE, warning=FALSE}
today <- c_data[c(1, 40)]
europe <- map_data("world", region = eu_countries)
c_data_long <- c_data %>%
gather(value=deaths, key=dates,2:101)
full <- left_join(europe, c_data_long, by= c("region" = "country"))
full$dates <- as.Date(full$dates, format = "%m/%d/%y")
```
```{r include=FALSE, warning=FALSE}
map_animation <- ggplot(full, aes(x=long, y=lat, group=group, fill=deaths)) +
geom_polygon( col="black") +
coord_fixed(ratio = 1) +
labs(title = "COVID-19 Deaths Over Time", subtitle = 'Date: {current_frame}') +
scale_fill_gradientn(colors = c("black", "red")) +
transition_manual(dates)
```
```{r, echo=FALSE, warning=FALSE}
animate(map_animation, duration = 10, fps = 20, renderer = gifski_renderer())
```