-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwassersteinDistro.R
194 lines (165 loc) · 6.7 KB
/
wassersteinDistro.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
# script for plane figures of rate, sampling, and dates patterns against W statistics
library(ggplot2)
library(viridis)
library(reshape)
library(cowplot)
library(tidyverse)
library(latex2exp)
library(ggpmisc)
library(ggExtra)
library(GGally)
library(ggtext)
path <- getwd()
figPath <- paste0(path, "/figures/")
load("wassersteinData.RData")
# traingles to shade regions
trsup <- data.frame(x = c(0, 0, 0.75), y = c(0, 0.75, 0.75))
trinf <- data.frame(x = c(0, 0.75, 0.75), y = c(0, 0, 0.75))
# baseline theme for plots
plotTheme <- function() {
theme_minimal() +
theme(legend.position = "bottom",
plot.margin = unit(c(0, 0, 0, 0), "cm")
)
}
# bar plot of classifications
barDat <- wassersteinData %>%
group_by(sampProp, rate)
rate.labs <- c((" Rate: 10^(-5) (subs/site/time)"), (" Rate: 10^(-3) (subs/site/time)"))
names(rate.labs) <- unique(wassersteinData$rate)
pBar <- ggplot(barDat) +
geom_bar(aes(x = as.character(sampProp), fill = class)) +
scale_fill_manual(name = "", values = alpha(viridis(2), 0.8)) +
xlab("Sampling Proportion") +
facet_wrap(~rate, labeller = labeller(rate = rate.labs)) +
coord_fixed(ratio = 0.027) +
plotTheme() +
theme(strip.text = element_markdown())
wassDist <- ggplot(wassersteinData, aes(x = wd, y = ws)) +
xlim(0, 0.75) + ylim(0, 0.75) +
# shading
geom_polygon(aes(x = x, y = y), data = trsup, fill = alpha("grey", 0.5)) +
geom_polygon(aes(x = x, y = y), data = trinf, fill = alpha("white", 0.5)) +
geom_quadrant_lines(linetype = "solid") +
# square plot
coord_fixed(ratio = 1) +
geom_point(pch = 21, size = 1.5, fill = alpha("steelblue", 0.6)) +
#scale_fill_manual(name = "Rate\n(subs/site/time)",
#values = alpha(rev(viridis(2)), 0.8),
#labels = c("10^-3", "10^-5")) +
geom_quadrant_lines(linetype = "solid") +
xlab(TeX("W_{D}")) + ylab(TeX("W_{S}")) +
annotate("text", x = c(0.25, 0.55), y = c(0.55, 0.25),
label = c("Date-Driven", "Seq-Driven"), size = 2.5) +
plotTheme()
wassDist <- ggMarginal(wassDist, size = 7, type = "histogram", fill = alpha("steelblue", 0.6))
# First row of panel plot
p1 <- plot_grid(wassDist, pBar, rel_widths = c(1, 1.8), labels = "AUTO")
tiff(file = paste0(figPath, 'wassDistandBar.tiff'),
units = "in", height = 3, width = 6, res = 300, compression = "lzw")
p1
dev.off()
# site pattern plot (second panel)
plotTheme <- function() {
theme_minimal() +
theme(legend.position = "bottom",
plot.margin = unit(c(0, 0, 0, -1), "cm"),
legend.title = element_text(size = 10),
#strip.text = element_text(size = 12),
#axis.title = element_text(size = 12)
)
}
sampProp.labs <- c("Sampling prop. = 0.05",
"Sampling prop. = 0.5",
"Sampling prop. = 1.0")
names(sampProp.labs) <- unique(wassersteinData$sampProp)
p2 <- ggplot(wassersteinData, aes(x = wd, y = ws, fill = log10(sitePatterns))) +
xlim(0, 0.75) + ylim(0, 0.75) +
# shading
geom_polygon(aes(x = x, y = y), data = trsup, fill = alpha("grey", 0.5)) +
geom_polygon(aes(x = x, y = y), data = trinf, fill = alpha("white", 0.5)) +
geom_quadrant_lines(linetype = "solid") +
geom_point(pch = 21, size = 2.5) +
coord_fixed(ratio = 1) +
scale_fill_continuous(name = TeX("log_{10}(Site Patterns)"), type = "viridis", breaks = 1:4) +
facet_wrap(~sampProp, labeller = labeller(sampProp = sampProp.labs)) +
facet_wrap(~sampProp, labeller = labeller(sampProp = sampProp.labs)) +
xlab(TeX("W_{D}")) + ylab(TeX("W_{S}")) +
annotate("text", x = c(0.25, 0.55), y = c(0.55, 0.25),
label = c("Date-Driven", "Seq-Driven"), size = 2.5) +
plotTheme()
# Site pattern plot normalised by sampling size
wassersteinData <- wassersteinData %>% mutate(relSP = sitePatterns / (sampProp * 500))
p2_1 <- ggplot(wassersteinData, aes(x = wd, y = ws, fill = relSP)) +
xlim(0, 0.75) + ylim(0, 0.75) +
# shading
geom_polygon(aes(x = x, y = y), data = trsup, fill = alpha("grey", 0.5)) +
geom_polygon(aes(x = x, y = y), data = trinf, fill = alpha("white", 0.5)) +
geom_quadrant_lines(linetype = "solid") +
geom_point(pch = 21, size = 2.5) +
coord_fixed(ratio = 1) +
scale_fill_continuous(name = TeX("\\frac{Site Patterns}{Num. Samples}"), type = "viridis", breaks = c(0, 1, 10), limits = c(0, 18)) +
facet_wrap(~sampProp, labeller = labeller(sampProp = sampProp.labs)) +
facet_wrap(~sampProp, labeller = labeller(sampProp = sampProp.labs)) +
xlab(TeX("W_{D}")) + ylab(TeX("W_{S}")) +
annotate("text", x = c(0.25, 0.55), y = c(0.55, 0.25),
label = c("Date-Driven", "Seq-Driven"), size = 2.5) +
plotTheme()
# Date span plot (third panel)
p3 <- ggplot(wassersteinData, aes(x = wd, y = ws, fill = datePatterns)) +
xlim(0, 0.75) + ylim(0, 0.75) +
# shading
geom_polygon(aes(x = x, y = y), data = trsup, fill = alpha("grey", 0.5)) +
geom_polygon(aes(x = x, y = y), data = trinf, fill = alpha("white", 0.5)) +
geom_quadrant_lines(linetype = "solid") +
geom_point(pch = 21, size = 2.5) +
coord_fixed(ratio = 1) +
#scale_fill_continuous(name = "Site Patterns", type = "viridis", breaks = seq(0, 4000, by = 500)) +
scale_fill_continuous(name = "Date Span ", type = "viridis") +
facet_wrap(~sampProp, labeller = labeller(sampProp = sampProp.labs)) +
xlab(TeX("W_{D}")) + ylab(TeX("W_{S}")) +
annotate("text", x = c(0.25, 0.55), y = c(0.55, 0.25),
label = c("Date-Driven", "Seq-Driven"), size = 2.5) +
plotTheme()
# combined plot
tiff(file = paste0(figPath, 'wassPanel.tiff'),
units = "in", height = 8, width = 6, res = 300, compression = "lzw")
ggpubr::ggarrange(p1, p2, p3, nrow = 3, labels = c("", "C", "D"), align = "v", heights = c(1, 1.1, 1.1))
dev.off()
# Date and seq pattern plots
tiff(file = paste0(figPath, 'wassSeqPat.tiff'),
units = "in", height = 3, width = 6, res = 300, compression = "lzw")
p2
dev.off()
pdf(file = paste0(figPath, "relSP.pdf"), height = 3, width = 6,
useDingbats = FALSE)
p2_1
dev.off()
tiff(file = paste0(figPath, 'wassDatePat.tiff'),
units = "in", height = 3, width = 6, res = 300, compression = "lzw")
p3
dev.off()
# Violin distribution of wass. Maybe move to supps
source('ggSplitViolin.R')
newDat <- wassersteinData %>%
pivot_longer(starts_with("w"), values_to='wasserstein', names_to='type', values_drop_na=T)
tiff(file = paste0(figPath, "wassViolin.tiff"))
ggplot(subset(newDat, type != "wn"),
aes(x = as.character(sampProp), y = wasserstein, fill = type)) +
geom_split_violin() +
scale_fill_manual(values = alpha(viridis(2), 0.8),
labels = c(expression(W[D]), expression(W[S]))) +
xlab("Sampling Proportion") + ylab(expression(W[D]~or~W[S])) +
theme_minimal() +
plotTheme() +
theme(
legend.title = element_blank()
)
dev.off()
## Plot of patterns vs class for supps
tiff(file = paste0(figPath, "patternsClass.tiff"))
ggplot(wassersteinData) +
geom_point(aes(x = sitePatterns, y = datePatterns, col = class)) +
facet_wrap(~rate + sampProp, scales = "free") +
scale_color_manual(name = "Classification", values = viridis(2))
dev.off()