-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcovidcomp_tw.Rmd
113 lines (106 loc) · 4.23 KB
/
covidcomp_tw.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
---
title: "Taiwan COVID-19"
output:
html_document:
df_print: paged
---
```{r}
options(conflicts.policy = "depends.ok")
library(dplyr)
library(tidyr)
library(readr)
library(stringr)
library(lubridate)
library(ggplot2)
daily_url <- "https://od.cdc.gov.tw/eic/Day_Confirmation_Age_County_Gender_19CoV.csv"
latest_cumulative_url <- "https://od.cdc.gov.tw/eic/covid19/covid19_tw_stats.csv"
tests_url <- "https://od.cdc.gov.tw/eic/covid19/covid19_tw_specimen.csv"
test_loc_url <- "https://od.cdc.gov.tw/icb/%e6%8c%87%e5%ae%9a%e6%8e%a1%e6%aa%a2%e9%86%ab%e9%99%a2%e6%b8%85%e5%96%ae(%e8%8b%b1%e6%96%87%e7%89%88).csv"
owid_url <- "https://covid.ourworldindata.org/data/owid-covid-data.csv"
```
```{r}
(daily_tests <- read_csv(tests_url, col_types = cols()) %>%
rename(date=通報日,
`home quarantine` = 居家檢疫送驗,
`expanded monitoring` = 擴大監測送驗,
`notifiable disease` = 法定傳染病通報,
`total tests` = Total) %>%
mutate(date=lubridate::ymd(date)) %>%
pivot_longer(-c(date), names_to = "test type", values_to = "tests")) %>%
filter(`test type` != "total tests") %>%
ggplot(aes(date, tests, fill = `test type`)) +
geom_col() +
theme(legend.title = element_blank(), legend.position = "bottom") +
ggtitle("COVID-19 Tests in Taiwan") +
facet_wrap(~ `test type`, scale = "free_y", ncol = 1)
```
```{r}
(tw_cv19 <- read_csv(daily_url, col_types = cols()) %>%
rename(
disease = 確定病名,
date = 個案研判日,
city = 縣市,
district = 鄉鎮,
gender = 性別,
age = 年齡層,
imported = 是否為境外移入,
`confirmed cases` = 確定病例數) %>%
mutate(
disease = recode(disease, 嚴重特殊傳染性肺炎 = "COVID-19"),
date = lubridate::ymd(date),
gender = recode(gender, 男 = "male", 女 = "female"),
imported = recode(imported, 是 = "true", 否 = "false")) %>%
left_join(daily_tests %>% pivot_wider(names_from = `test type`,
values_from = tests), by = "date")) %>%
mutate(across(where(is.character), as.factor)) %>% summary()
```
```{r}
tw_cv19 %>%
mutate(date = lubridate::floor_date(date, "week"),
city = case_when(imported == "true" ~ "imported",
!str_ends(city, "北市") ~ "other city",
TRUE ~ city)) %>%
group_by(date, city) %>%
summarise(across(`confirmed cases`, sum), .groups = "drop") %>%
filter(date >= '2021-01-01') %>%
ggplot(aes(date, `confirmed cases`, fill = city)) +
geom_col(position = "dodge") +
facet_wrap(~ city, ncol = 1)
```
```{r}
tw_cv19 %>%
group_by(disease, date) %>%
summarise(across(`confirmed cases`, sum), across(`total tests`, head, 1),
.groups = "drop") %>%
mutate(date = lubridate::floor_date(date, "week")) %>%
group_by(disease, date) %>%
summarise(across(everything(), sum), .groups = "drop") %>%
mutate(`positive test rate` = `confirmed cases` / `total tests`) %>%
pivot_longer(-c(disease, date)) %>%
ggplot(aes(date, value, group = name)) +
geom_col() + facet_wrap(~name, scales = "free_y", ncol = 1)
```
```{r}
p <- read_csv("https://covid.ourworldindata.org/data/owid-covid-data.csv",
col_types = cols(icu_patients = "d", hosp_patients = "d",
weekly_hosp_admissions = "d",
weekly_hosp_admissions_per_million = "d",
weekly_icu_admissions = "d",
weekly_icu_admissions_per_million = "d",
icu_patients_per_million = "d",
hosp_patients_per_million = "d")) %>%
filter(location == "Taiwan" & date >= "2021-04-15") %>%
select(date:stringency_index, -tests_units, -people_vaccinated) %>%
pivot_longer(-date) %>%
filter(!str_detect(name, "_per_")) %>%
group_by(name) %>%
filter(!all(is.na(value))) %>%
ungroup() %>%
ggplot(aes(date, value, fill = name)) +
geom_col() + facet_wrap(~ name, scale = "free_y", ncol = 4) +
theme(legend.position = "none", panel.spacing=unit(0,'npc')) #strip.text.x = element_text(size = 12))
(gp <- plotly::partial_bundle(plotly::ggplotly(p)))
ggsave("tw_c19.png", plot = p, width = 26, height = 14, units = "in",
dpi = "screen")
htmlwidgets::saveWidget(gp, "tw_c19.html")
```