-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcovid19_eda.R
416 lines (347 loc) · 15 KB
/
covid19_eda.R
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# Load packages
library(tidyverse)
library(lubridate)
library(plotly)
library(leaflet)
# Read data
covid19_global_cases <- 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")
covid19_global_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")
covid19_global_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")
covid19_global_cases %>% View()
covid19_global_deaths %>% View()
covid19_global_recovered %>% View()
# Check to ensure cases, deaths, and recovered data has same column names
all.equal(colnames(covid19_global_cases), colnames(covid19_global_deaths), colnames(covid19_global_recovered))
# Function to convert covid19 data to long format
convert_covid_long <- function(data, col_name) {
data %>%
rename(province_state = `Province/State`,
country_region = `Country/Region`) %>%
pivot_longer(-c(province_state,country_region,Lat,Long),
names_to = "date",
values_to = col_name)
}
# Convert covid19 data to long format
global_cases_long <- convert_covid_long(covid19_global_cases, "confirmed_cases")
global_deaths_long <- convert_covid_long(covid19_global_deaths, "confirmed_deaths")
global_recovered_long <- convert_covid_long(covid19_global_recovered, "confirmed_recovered")
# Join cases, deaths, and recovered data
covid19_global_full <- global_cases_long %>%
full_join(global_deaths_long) %>%
full_join(global_recovered_long) %>%
mutate(date = mdy(date),
## Make edits to regions names for geo joining
country_region = case_when(country_region == "Brunei" ~ "Brunei Darussalam",
country_region == "Cote d'Ivoire" ~ "Côte d'Ivoire",
country_region == "Czechia" ~ "Czech Republic",
country_region == "Korea, South" ~ "Republic of Korea",
country_region == "Laos" ~ "Lao PDR",
country_region == "North Macedonia" ~ "Macedonia",
country_region == "Russia" ~ "Russian Federation",
country_region == "Taiwan*" ~ "Taiwan",
country_region == "US" ~ "United States",
country_region == "West Bank and Gaza" ~ "Palestine",
# New names
country_region == "Burma" ~ "Myanmar",
country_region == "Congo (Brazzaville)" ~ "Republic of Congo",
country_region == "Congo (Kinshasa)" ~ "Democratic Republic of the Congo",
# Greenland overrules denmark for geographic location
province_state == "Greenland" ~ "Greenland",
TRUE ~ country_region)
)
global_cases_long %>% View()
covid19_global_full %>% View()
# covid19_global_full %>%
# filter(country_region == "Canada") %>% View()
#
# covid19_global_full %>%
# filter(date == "2020-01-22", is.na(confirmed_cases))
### Dashboard V1
# Total cases/deaths/recovered over time
covid19_sum <- covid19_global_full %>%
group_by(date) %>%
summarize(sum_cases = sum(confirmed_cases, na.rm = TRUE),
sum_deaths = sum(confirmed_deaths, na.rm = TRUE),
sum_recovered = sum(confirmed_recovered, na.rm = TRUE))
plotly_covid19_sum <- covid19_sum %>%
ggplot(aes(x = date,
y = sum_cases,
group = 1,
text = sprintf("Total Cases: %s", sum_cases))) +
geom_line(color = "dodgerblue", size = 1) +
scale_y_continuous(labels = scales::comma) +
theme_light() +
labs(title = "Total Number of COVID-19 ____ Worldwide",
x = "",
y = "Number of ____")
ggplotly(plotly_covid19_sum, tooltip = "text")
plotly_region <- covid19_global_full %>%
filter(country_region == "United Kingdom") %>%
group_by(date) %>%
summarize(sum_cases = sum(confirmed_cases, na.rm = TRUE),
sum_deaths = sum(confirmed_deaths, na.rm = TRUE),
sum_recovered = sum(confirmed_recovered, na.rm = TRUE)) %>%
ggplot(aes(x = date,
y = sum_cases,
group = 1,
text = sprintf("Total Cases: %s<br>Date: %s", scales::comma(sum_cases, accuracy = 1), date))) +
geom_line() +
scale_y_continuous(labels = scales::comma) +
expand_limits(y = 0) +
theme_light() +
labs(title = sprintf("%s cases over time", "United Kingdom"),
x = "",
y = "Number of cases")
ggplotly(plotly_region, tooltip = "text")
# List of confirmed cases/deaths/recovered by country/region
covid19_global_current <- covid19_global_full %>%
filter(date == max(date)) %>%
group_by(country_region) %>%
summarize(current_cases = sum(confirmed_cases, na.rm = TRUE),
current_deaths = sum(confirmed_deaths, na.rm = TRUE),
current_recovered = sum(confirmed_recovered, na.rm = TRUE)) %>%
right_join(distinct(covid19_global_full, country_region))
covid19_global_current %>% View()
# Look at histogram dist for map legend ranges
options(scipen=999)
covid19_global_current$current_cases %>% cut(8) %>% levels()
covid19_global_current %>%
ggplot(aes(x = current_recovered)) +
geom_histogram(bins = 30) +
scale_x_log10(labels = scales::comma,
breaks = c(0,10,50,100,500,1000,5000,10000,100000))
# Number of total worldwide confirmed cases/deaths/recovered
covid19_global_current %>%
summarize(total_cases = sum(current_cases, na.rm = TRUE),
total_deaths = sum(current_deaths, na.rm = TRUE),
total_recovered = sum(current_recovered, na.rm = TRUE))
covid19_global_full %>%
distinct(country_region)
# New cases per day ------------------------------------------------------------------------------
covid19_global_daily <- covid19_global_full %>%
group_by(country_region, date) %>%
summarize(sum_cases = sum(confirmed_cases, na.rm = TRUE),
sum_deaths = sum(confirmed_deaths, na.rm = TRUE),
sum_recovered = sum(confirmed_recovered, na.rm = TRUE)) %>%
mutate(diff_cases = sum_cases - lag(sum_cases),
diff_deaths = sum_deaths - lag(sum_deaths),
diff_recovered = sum_recovered - lag(sum_recovered))
covid19_daily %>% View()
covid19_daily %>%
filter(country_region == "France") %>%
ggplot(aes(x = date, y = diff_cases)) +
geom_col() +
theme_light()
# Map: Cases/deaths/recovered by country/region
library(leaflet)
library(tigris)
library(rnaturalearth)
library(rnaturalearthdata)
# GEO JOINING -------------------------------------------
ne_t <- ne_countries(type = "tiny_countries")
ne_s <- ne_countries(type = "sovereignty")
ne_u <- ne_countries(type = "map_units")
ne_c <- ne_countries()
ne_t@data$name_long %>% View()
ne_s@data$name_long
ne_u@data$name_long %>% View()
ne_c@data$name_long %>% View()
library(maptools)
class(ne_c@polygons )
rbind.SpatialPointsDataFrame(ne_c, ne_t)
ne_ct$name_long %>% View()
data.frame(name = ne_u@data$name_long) %>%
anti_join(data.frame(name = ne_c@data$name_long ))
### join tiny & unit and unit & country to see what regions are mission from each other
### use complete region data to join on covid19_global_current
countries <- ne_countries()
covid19_global_geo <- geo_join(countries, covid19_global_current, "name_long", "country_region")
covid19_global_geo
x <- covid19_global_current %>%
mutate(country_region2 = country_region) %>%
select(country_region, country_region2)
covid19_global_geo@data %>% select(name_long, country_region) %>% View()
covid19_global_geo@data %>%
select(name_long) %>%
full_join(x, by = c("name_long" = "country_region"), keep = TRUE) %>%
View()
countries@data %>%
select(name_long) %>%
full_join(x, by = c("name_long" = "country_region"), keep = TRUE) %>%
# filter(is.na(country_region2)) %>%
View()
x %>%
select(country_region) %>%
full_join(mutate(data.frame(name_long = countries@data$name_long), name_long2 = name_long),
by = c("country_region" = "name_long")) %>%
# filter(is.na(name_long2)) %>%
View()
countries_not_in_x <- countries@data %>%
select(name_long) %>%
full_join(x, by = c("name_long" = "country_region"), keep = TRUE) %>%
filter(is.na(country_region2))
countries_not_in_x %>% View()
countries_not_in_ne <- x %>%
select(country_region) %>%
full_join(mutate(data.frame(name_long = countries@data$name_long), name_long2 = name_long), by = c("country_region" = "name_long"), keep = TRUE) %>%
filter(is.na(name_long2))
countries_not_in_ne %>% View()
ne_country_list <- data.frame(name_long = countries@data$name_long) %>%
bind_cols(data.frame(name = countries@data$name))
# Leaflet mapping ---------------------
covid19_global_geo@data$current_cases
covid19_global_geo@data %>%
# filter(current_cases < 10000) %>%
ggplot(aes(x = current_cases)) +
geom_histogram(bins = 100)
library(RColorBrewer)
mybins <- c(0,10,100,500,1000,5000,10000,50000,100000,Inf)
mypalette <- colorBin(palette = "YlOrBr",
domain = covid19_global_geo@data$current_cases,
na.color = "transparent",
bins = mybins)
# Prepare the text for tooltips:
mytext <- paste(
"Country/region: ", covid19_global_geo@data$name_long,"<br/>",
"Current cases: ", covid19_global_geo@data$current_cases, "<br/>",
sep="") %>%
lapply(htmltools::HTML)
# Final Map
leaflet(covid19_global_geo) %>%
addTiles() %>%
setView( lat=10, lng=0 , zoom=2) %>%
addPolygons(
fillColor = ~mypalette(current_cases),
stroke=TRUE,
fillOpacity = 0.9,
color="white",
weight=0.3,
label = mytext,
labelOptions = labelOptions(
style = list("font-weight" = "normal", padding = "3px 8px"),
textsize = "13px",
direction = "auto"
)
) %>%
addLegend(pal = mypalette,
values = ~current_cases,
opacity = 0.9,
title = "Current cases",
position = "bottomleft")
# plotly
library(geojsonio)
spdf <- geojson_read("https://raw.githubusercontent.com/gregoiredavid/france-geojson/master/communes.geojson", what = "sp")
spdf$code
spdf_fortified <- tidy(spdf, region = "code")
data <- read.table("https://www.r-graph-gallery.com/wp-content/uploads/2017/12/data_on_french_states.csv", header=T, sep=";")
head(data)
### Testing other choropleth mapping options --------
library(broom)
library(mapproj)
covid19_global_geo@data %>% View()
covid_geo_fortified <- tidy(covid19_global_geo)
ggplot() +
geom_polygon(data = covid19_global_geo, aes(x = long, y = lat, group = group), color="grey") +
theme_void() +
coord_map()
library(maps)
world_map <- map("world", fill = TRUE, plot = FALSE)
world_map
map('world',col="grey", fill=TRUE, bg="white", lwd=0.05, mar=rep(0,4),border=0, ylim=c(-80,80) )
map('world', fill = TRUE, col = 1:10)
mapStates = map("state", fill = TRUE, plot = FALSE)
str(mapStates)
leaflet(data = mapStates) %>%
addProviderTiles(provider = "CartoDB") %>%
addPolygons(fillColor = topo.colors(10, alpha = NULL), stroke = FALSE)
####### US
# Read data
covid19_us_cases <- read_csv("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_US.csv")
covid19_us_deaths <- read_csv("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_US.csv")
covid19_us_cases %>% View()
covid19_us_deaths %>% View()
all.equal(colnames(covid19_us_cases), colnames(covid19_us_deaths))
# Convert us data to long format
us_cases_long <- covid19_us_cases %>%
rename(province_state = Province_State,
country_region = Country_Region,
Long = Long_) %>%
select(-c(UID, iso2, iso3, code3, FIPS, Admin2, Combined_Key)) %>%
pivot_longer(-c(province_state,country_region,Lat,Long),
names_to = "date",
values_to = "confirmed_cases")
us_deaths_long <- covid19_us_deaths %>%
rename(province_state = Province_State,
country_region = Country_Region,
Long = Long_) %>%
select(-c(UID, iso2, iso3, code3, FIPS, Admin2, Combined_Key)) %>%
pivot_longer(-c(province_state,country_region,Lat,Long,Population),
names_to = "date",
values_to = "confirmed_deaths")
# Join cases, deaths, and recovered us data
covid19_us_full <- us_cases_long %>%
full_join(us_deaths_long) %>%
mutate(date = mdy(date))
# List of confirmed cases/deaths/recovered by state
covid19_us_current <- covid19_us_full %>%
filter(date == max(date),
!province_state %in% c("Diamond Princess", "Grand Princess")) %>%
group_by(province_state) %>%
summarize(current_cases = sum(confirmed_cases, na.rm = TRUE),
current_deaths = sum(confirmed_deaths, na.rm = TRUE))
covid19_us_full %>% distinct(province_state) %>% View()
# Total cases/deaths/recovered over time
covid19_sum <- covid19_us_full %>%
group_by(date) %>%
summarize(sum_cases = sum(confirmed_cases, na.rm = TRUE),
sum_deaths = sum(confirmed_deaths, na.rm = TRUE))
# EDA HISTOGRAM
covid19_us_current %>%
ggplot(aes(x = current_deaths)) +
geom_histogram(bins = 30) +
scale_x_log10(labels = scales::comma,
breaks = c(0,10,50,100,500,1000,2500,5000))
# US MAP
# Get geo US data
# devtools::install_github("ropensci/rnaturalearthhires")
library(rnaturalearthhires)
us_states <- ne_states(country = "United States of America")
covid19_us_geo <- geo_join(us_states, covid19_us_current, "name", "province_state")
us_cases_bins <- c(0,1000,5000,10000,20000,40000,Inf)
us_cases_palette <- colorBin(palette = "viridis",
reverse = TRUE,
domain = covid19_us_geo@data$current_cases,
na.color = "transparent",
bins = us_cases_bins)
# Prepare the text for tooltips:
us_cases_text <- paste(
"State: ", covid19_us_geo@data$name,"<br/>",
"Current cases: ", scales::comma(covid19_us_geo@data$current_cases), "<br/>",
sep="") %>%
lapply(htmltools::HTML)
# US Map
leaflet(covid19_us_geo) %>%
addTiles() %>%
setView(lat=50, lng=-125 , zoom=3) %>%
# setMaxBounds(lng1 = -180,
# lat1 = 85.05115,
# lng2 = 180,
# lat2 = -85.05115) %>%
addPolygons(
fillColor = ~us_cases_palette(current_cases),
stroke=TRUE,
fillOpacity = 0.7,
color="white",
weight=0.3,
label = us_cases_text,
labelOptions = labelOptions(
style = list("font-weight" = "normal", padding = "3px 8px"),
textsize = "13px",
direction = "auto"
)
) %>%
addLegend(pal = us_cases_palette,
values = ~current_cases,
opacity = 0.9,
title = "Current cases",
position = "bottomleft")