-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_cluster_charact_over_time.R
99 lines (88 loc) · 4.79 KB
/
plot_cluster_charact_over_time.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
## This script enables to reproduce the figure describing the characteristics of clusters
## of identical sequences during our study period in WA.
library(tidyverse)
## Load dataframe with number of clusters by week of first cluster detection
## Week 0 corresponds to 1 Mar 2021 - 7 Mar 2021
df_cluster_charact <- read_csv('../../results/cluster_identical_sequences/charact_clusters_by_detection_week.csv')
## Plot number of new clusters per week, mean size of clusters and
### Characteristic of secondary axis
scaling_slope_y_axis <- 1100
scaling_intercept_y_axis <- 1100
### Characteristic of tertiary axis
scaling_slope_y_axis_2 <- 85
scaling_intercept_y_axis_2 <- 0
### Breaks for third axis
breaks_cluster_duration <- c(0, 5, 10, 15, 20, 25)
scaled_breaks_cluster_duration <- breaks_cluster_duration * scaling_slope_y_axis_2 - scaling_intercept_y_axis_2
date_start_plot <- as.Date('2021-03-01')
plt_cluster_charact_over_time <- df_cluster_charact %>%
ggplot(aes(x = week)) +
geom_bar(aes(y = n_clusters), stat = 'identity', fill = 'gray') +
geom_line(aes(y = mean_cluster_size * scaling_slope_y_axis - scaling_intercept_y_axis),
color = 'orange2') +
geom_line(aes(y = mean_cluster_duration * scaling_slope_y_axis_2 - scaling_intercept_y_axis_2),
color = 'darkcyan') +
annotate("segment", x = 109, y = 0, xend = 109, yend = 2300, color = 'darkcyan') +
annotate("text", x = 115, y = 1150, color = 'darkcyan', angle = -90, hjust = 0.5,
label = 'Mean cluster duration (in days)') +
annotate('segment', x = 109, xend = 109.5,
y = scaled_breaks_cluster_duration[1], yend = scaled_breaks_cluster_duration[1],
color = 'darkcyan') +
annotate('segment', x = 109, xend = 109.5,
y = scaled_breaks_cluster_duration[2], yend = scaled_breaks_cluster_duration[2],
color = 'darkcyan') +
annotate('segment', x = 109, xend = 109.5,
y = scaled_breaks_cluster_duration[3], yend = scaled_breaks_cluster_duration[3],
color = 'darkcyan') +
annotate('segment', x = 109, xend = 109.5,
y = scaled_breaks_cluster_duration[4], yend = scaled_breaks_cluster_duration[4],
color = 'darkcyan') +
annotate('segment', x = 109, xend = 109.5,
y = scaled_breaks_cluster_duration[5], yend = scaled_breaks_cluster_duration[5],
color = 'darkcyan') +
annotate('segment', x = 109, xend = 109.5,
y = scaled_breaks_cluster_duration[6], yend = scaled_breaks_cluster_duration[6],
color = 'darkcyan') +
annotate('text', x = 110, y = scaled_breaks_cluster_duration[1],
color = 'darkcyan', label = breaks_cluster_duration[1],
hjust = 0) +
annotate('text', x = 110, y = scaled_breaks_cluster_duration[2],
color = 'darkcyan', label = breaks_cluster_duration[2],
hjust = 0) +
annotate('text', x = 110, y = scaled_breaks_cluster_duration[3],
color = 'darkcyan', label = breaks_cluster_duration[3],
hjust = 0) +
annotate('text', x = 110, y = scaled_breaks_cluster_duration[4],
color = 'darkcyan', label = breaks_cluster_duration[4],
hjust = 0) +
annotate('text', x = 110, y = scaled_breaks_cluster_duration[5],
color = 'darkcyan', label = breaks_cluster_duration[5],
hjust = 0) +
annotate('text', x = 110, y = scaled_breaks_cluster_duration[6],
color = 'darkcyan', label = breaks_cluster_duration[6],
hjust = 0) +
scale_x_continuous(name = 'Week of first cluster detection',
breaks = c(0, 15, 30, 45, 60, 75, 90),
labels = format(date_start_plot + c(0, 15, 30, 45, 60, 75, 90) * 7, '%d %b\n%Y'),
expand = expansion(mult = c(0.03, 0.03))) +
scale_y_continuous(name = 'Number of new clusters',
expand = expansion(mult = c(0., 0.1)),
sec.axis = sec_axis(~ (. + scaling_intercept_y_axis)/ scaling_slope_y_axis,
name = 'Mean cluster size')) +
theme_classic() +
coord_cartesian(xlim = c(0, 95), ylim = c(0, 2100), clip = "off") +
theme(axis.text = element_text(size = 12),
axis.title = element_text(size = 12),
axis.text.y.right = element_text(colour = 'orange2'),
axis.title.y.right = element_text(colour = 'orange2'),
axis.line.y.right = element_line(colour = 'orange2'),
axis.ticks.y.right = element_line(colour = 'orange2'),
plot.margin = unit(c(1,4,1,1), "lines"))
pdf('../../figures/supplementary_figures/charact_clusters_over_time.pdf',
height = 3., width = 8)
plot(plt_cluster_charact_over_time)
dev.off()
png('../../figures/supplementary_figures/charact_clusters_over_time.png',
height = 3., width = 8, res = 350, units = 'in')
plot(plt_cluster_charact_over_time)
dev.off()