-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFig2c_heatmap_tianjin.Rmd
114 lines (90 loc) · 3.94 KB
/
Fig2c_heatmap_tianjin.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: "Progression Heatmap Data Preprocessing & Plotting"
author: "Venus Lau; Yen-Hsiang (Brian) Lee"
date: "19/02/2020"
updated: "23/02/2020"
output:
html_document:
keep_md: TRUE
---
#Preprocessing: formatting table for heatmap
```{r setup, include=FALSE}
library(tidyverse)
data<-read.table("data/COVID-19_Tianjin_Heatmap-table.csv", header = TRUE, sep = ",")
#data_long <- data %>% gather(key=date, value=status, X1.18.2020:X2.21.2020)
data_long <- data %>% gather(key=date, value=status, starts_with("X"))
data_long$date <- gsub('X', '0', data_long$date)
data_long$date <- gsub('\\.', '\\/', data_long$date)
data_long$date <- as.Date(data_long$date, "%m/%d/%Y")
#write.csv(data_long, "data/tianjin_heatmap_long.csv", row.names=FALSE)
```
# Plotting
```{r }
library(ggplot2)
library(viridis)
library(plotly)
data <- read.csv("data/tianjin_heatmap_long.csv")
data$date <- factor(data$date, levels=unique(data$date))
data$case <- factor(data$case, levels=unique(data$case))
data$status_word=ifelse(data$status == 0,"Unexposed",
ifelse(data$status == 1,"Exposed",
ifelse(data$status == 2,"Symptomatic",
ifelse(data$status == 3,"Confirmed","Dead"))))
#write.csv(data, "data/COVID-19_Tianjin_Heatmap_plot.csv")
```
```{r }
## Manually set the colours for the heatmap so that they are consistent with the Singapore heatmap
# used scales::viridis_pal()(5) to check the colours used in the Singapore heatmap
# and assigned the colours to the infection statuses accordingly
group.colors <- c(Unexposed="#440154FF", Symptomatic="#3B528BFF", Confirmed="#21908CFF", Exposed="#5DC863FF", Dead="#FDE725FF")
p1 <- ggplot(
data,
# aes(x = date, y = case, fill = status_word,
aes(x = date, y = case, fill = status,
text = paste("Case: ", case_detailed,
"<br>Date: ", date,
"<br>Status: ", status_word,
"<br>Cluster: ", cluster)))+#,
#"<br>Citizenship: ", citizenship))) +
geom_tile() +
xlab(label = "Date") +
ylab(label = "Cases") +
ggtitle("COVID-19 Progression Amongst Tianjin Cases") +
labs(fill = "Status") + #tile fill legend label
theme(plot.title = element_text(hjust = 0.5)) + #centre main title
theme(axis.text.x = element_text(angle = 60, hjust = 0.6, size = 8),
axis.ticks.x = element_blank(), #remove x axis ticks
axis.ticks.y = element_blank()) + #remove y axis ticks
# scale_fill_viridis_d(direction = -1) +
scale_fill_viridis_c(direction = 1) +
theme(panel.background = element_rect(fill = "white"))
ggplotly(p1,tooltip = 'text')
```
```{r }
p_static=ggplot(
data,
# aes(x = date, y = case, fill = status_word,
aes(x = date, y = case, fill = status_word ,
text = paste("Case: ", case_detailed,
"<br>Date: ", date,
"<br>Status: ", status_word,
"<br>Cluster: ", cluster )))+#,
# "<br>Citizenship: ", citizenship))) +
geom_tile() +
xlab(label = "Date") +
ylab(label = "Cases") +
ggtitle("COVID-19 Progression Amongst Tianjin Cases") +
labs(fill = "Status") + #tile fill legend label
theme(plot.title = element_text(hjust = 0.5)) + #centre main title
theme(axis.text.x = element_text(angle = 60, hjust = 0.6, size = 8),
axis.ticks.x = element_blank(), #remove x axis ticks
axis.ticks.y = element_blank()) + #remove y axis ticks
# scale_fill_viridis_d(direction = -1) +
#scale_fill_viridis_d(direction = -1,breaks=c("Unexposed","Exposed","Symptomatic","Confirmed","Dead")) +
scale_fill_manual(values=group.colors, breaks=c("Unexposed","Exposed","Symptomatic","Confirmed","Dead")) +
theme(panel.background = element_rect(fill = "white"),
axis.text.y = element_text(size=6),
axis.text.x = element_text(hjust=1))
p_static
#ggsave("final_figures/Fig2c_heatmap_tianjin.pdf",plot=p_static, device="pdf",width = 10,height = 11,units="in")
```