-
Notifications
You must be signed in to change notification settings - Fork 3
/
analysis.Rmd
144 lines (117 loc) · 4.86 KB
/
analysis.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
---
title: "Analysis of Covid-19"
author: "Jakob"
date: "1 4 2020"
output:
html_document:
toc: true
toc_float: true
df_print: paged
code_folding: show
theme: united
highlight: tango
number_sections: true
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(
echo = TRUE,
message = FALSE,
warning = FALSE
)
library(tidyverse)
```
# Getting Data
```{r}
df_confirmed <- read_csv("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv")
df_deaths <- read_csv("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_global.csv")
df_recovered <- read_csv("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_recovered_global.csv")
df_confirmed <- df_confirmed%>%
pivot_longer(cols = c(-`Province/State`, -`Country/Region`, -Lat, -Long), names_to = "Date")
df_confirmed <- rename(df_confirmed, Confirmed = value)
df_deaths <- df_deaths%>%
pivot_longer(cols = c(-`Province/State`, -`Country/Region`, -Lat, -Long), names_to = "Date")
df_recovered <- df_recovered%>%
pivot_longer(cols = c(-`Province/State`, -`Country/Region`, -Lat, -Long), names_to = "Date")
covid <- df_confirmed %>%
left_join(select(df_deaths, -Lat, -Long, Deaths = value))%>%
left_join(select(df_recovered, -Lat, -Long, Recovered = value))
covid$Date <- lubridate::mdy(covid$Date)
covid <- covid%>%
group_by(`Country/Region`, `Province/State`)%>%
mutate(new_confirmed = Confirmed - lag(Confirmed, n=1, order_by = Date))%>%
mutate(new_deaths = Deaths - lag(Deaths, n=1, order_by = Date))%>%
mutate(new_recovered = Recovered - lag(Recovered, n=1, order_by = Date))%>%
ungroup()
covid[4000:4005, ]
```
# Analyse Data
```{r}
q<- covid%>%
ungroup()%>%
group_by(Date)%>%
summarise(Confirmed = sum(Confirmed), Recovered=sum(Recovered, na.rm=TRUE), Deaths=sum(Deaths))%>%
ggplot(aes(Date, `All Cases`))+
geom_col(aes(x = Date, y = Confirmed))+
geom_col(aes(x = Date, y = Recovered), fill = "green")+
geom_col(aes(x = Date, y = Deaths), fill = "red")+
labs(title = "Total Cases Worldwide")
plotly::ggplotly(q)
covid%>%
filter(`Country/Region` %in% c("Germany", "Italy", "Spain", "US"))%>%
ggplot(aes(Date, `New Cases`))+
geom_col(aes(x = Date, y= new_confirmed))+
geom_line(aes(x = Date, y= new_recovered), color = "green", size = 1)+
geom_line(aes(x = Date, y= new_deaths), color = "red", size = 1)+
labs(title = "Daily Reportet New Cases")+
facet_wrap(~ `Country/Region`)
# alt.: facet_grid(rows = vars(`Country/Region`))
```
## New confirmed vs. new recovered
```{r}
covid%>%
group_by(`Country/Region`, Date) %>%
summarise(new_confirmed = sum(new_confirmed),
new_recovered=sum(new_recovered, na.rm=TRUE)) %>%
filter(`Country/Region` %in% c("Germany", "US", "Italy", "Austria", "China", "Korea, South"),
Date > '2020-02-01') %>%
pivot_longer(cols = starts_with("new"), names_to = "type", values_to = "val") %>%
ggplot()+
geom_line(aes(Date, val, color = type), size = 1) +
facet_wrap(~ `Country/Region`, scales = "free_y") +
theme(legend.position = "bottom")
```
## Active cases
```{r}
covid <- covid %>% mutate(Active = Confirmed - Deaths - Recovered)
grouped <- covid %>%
group_by(Date, `Country/Region`) %>%
summarise(Active = sum(Active, na.rm = TRUE)) %>%
filter(Active > 10)
countrylist <- c("Germany", "US", "China", "Turkey", "Korea, South", "Italy", "Russia", "Japan")
grouped %>%
ggplot(aes(Date, Active)) +
geom_line(aes(group = `Country/Region`), color = "grey") +
geom_line(data = filter(grouped, `Country/Region` %in% countrylist),
aes(color =`Country/Region`), size = 2) +
scale_y_log10(labels = scales::comma, n.breaks = 8) +
scale_color_brewer(type = "qual")
```
## Comparing steepness of curves across countries
```{r}
grouped <- grouped %>%
filter(Active>=500) %>%
group_by(`Country/Region`) %>%
mutate(days_since_100_active_cases = Date - min(Date))
countrylist <- c("Germany", "US", "China", "Turkey", "Korea, South", "Italy", "Japan", "Austria")
grouped %>%
ggplot(aes(days_since_100_active_cases, Active)) +
geom_line(aes(group = `Country/Region`), color = "grey") +
geom_line(data = filter(grouped, `Country/Region` %in% countrylist),
aes(color =`Country/Region`), size = 2) +
geom_text(data = filter(grouped, `Country/Region` %in% countrylist, Date==max(Date)),
aes(label = `Country/Region`, color =`Country/Region`), size = 5,hjust = "left") +
scale_y_log10(labels = scales::comma, n.breaks = 8) +
scale_color_brewer(type = "qual") +
theme(legend.position = "none") +
labs(x = "Days (starting with the first time that a country reports 100 active cases)")
```