-
Notifications
You must be signed in to change notification settings - Fork 3
/
notebook06-2D-and-3D-heatmaps.Rmd
328 lines (225 loc) · 14.7 KB
/
notebook06-2D-and-3D-heatmaps.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
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
---
title: "2D and 3D Heatmaps of time spent in locations"
output: html_notebook
---
```{r}
rm(list=ls(all=TRUE))
load("C:\\Users\\Nicole Hellessey\\Documents\\Post-doc\\Data\\Total Merged Data File (April 5 2022).Rdata")
head(TotalData) ## all rows of data from TotalData
head(CC.TotalData) ## ONLY complete cases of TotalData
## 2D heatmaps of time spent in locations
## Looking at differences by treatment
## start by plotting scatterplot colour by denisty of points
library(scatterplot3d)
scatterplot3d(TotalData$X, TotalData$Y, TotalData$Z, color = TotalData$Flow.rate, pch = 20, xlab = "X Axis (m)", ylab = "Y Axis (m)", zlab = "Z Axis (m)", cex.symbols = 0.5, angle = 55)
```
Now subsetting for Chlorophyll
```{r}
## Will subset Totaldata into each set of variables and try plotting this again
ChlA0 <- CC.TotalData[CC.TotalData$Chlorophyll == "0",] ##subsets CC.TotalData by Chlorophyll but only selects rows where Chlorophyll = 0
head(ChlA0)
tail(ChlA0)
ChlA4.3 <- CC.TotalData[CC.TotalData$Chlorophyll == "4.3",] ##subsets CC.TotalData by Chlorophyll but only selects rows where Chlorophyll = 4.3
head(ChlA4.3)
tail(ChlA4.3)
ChlA4.6 <- CC.TotalData[CC.TotalData$Chlorophyll == "4.6",] ##subsets CC.TotalData by Chlorophyll but only selects rows where Chlorophyll = 4.6
head(ChlA4.6)
tail(ChlA4.6)
ChlA6 <- CC.TotalData[CC.TotalData$Chlorophyll == "6.1",] ##subsets CC.TotalData by Chlorophyll but only selects rows where Chlorophyll = 6.1
head(ChlA6)
tail(ChlA6)
ChlA7 <- CC.TotalData[CC.TotalData$Chlorophyll == "7.6",] ##subsets CC.TotalData by Chlorophyll but only selects rows where Chlorophyll = 7.6
head(ChlA7)
tail(ChlA7)
ChlA13 <- CC.TotalData[CC.TotalData$Chlorophyll == "13.5",] ##subsets CC.TotalData by Chlorophyll but only selects rows where Chlorophyll = 13.5
head(ChlA13)
tail(ChlA13)
ChlA19 <- CC.TotalData[CC.TotalData$Chlorophyll == "19",] ##subsets CC.TotalData by Chlorophyll but only selects rows where Chlorophyll = 19
head(ChlA19)
tail(ChlA19)
## need to make a list of color for each level of Flow rate
levels(CC.TotalData$Flow.rate)
spcol <- c("lightblue", "blue", "red", "darkblue", "black")
## 3D scatterplot, colored by Flow.Rate BUT only points with 0 Chlorophyll
library(scatterplot3d)
scatterplot3d(ChlA0$X, ChlA0$Y, ChlA0$Z, color = spcol[as.numeric(ChlA0$Flow.rate)], pch = 20, xlab = "X Axis (m)", ylab = "Y Axis (m)", zlab = "Z Axis (m)", main = "Chlorophyll = 0 mg/L, colour = Flow Rate", cex.symbols = 0.01, angle = 55)
scatterplot3d(ChlA4.3$X, ChlA4.3$Y, ChlA4.3$Z, color = spcol[as.numeric(ChlA4.3$Flow.rate)], pch = 20, xlab = "X Axis (m)", ylab = "Y Axis (m)", zlab = "Z Axis (m)", main = "Chlorophyll = 4.3 mg/L, colour = Flow Rate", cex.symbols = 0.5, angle = 55)
scatterplot3d(ChlA4.6$X, ChlA4.6$Y, ChlA4.6$Z, color = spcol[as.numeric(ChlA4.6$Flow.rate)], pch = 20, xlab = "X Axis (m)", ylab = "Y Axis (m)", zlab = "Z Axis (m)", main = "Chlorophyll = 4.6 mg/L, colour = Flow Rate", cex.symbols = 0.5, angle = 55)
scatterplot3d(ChlA6$X, ChlA6$Y, ChlA6$Z, color = spcol[as.numeric(ChlA6$Flow.rate)], pch = 20, xlab = "X Axis (m)", ylab = "Y Axis (m)", zlab = "Z Axis (m)", main = "Chlorophyll = 6.1 mg/L, colour = Flow Rate", cex.symbols = 0.5, angle = 55)
scatterplot3d(ChlA7$X, ChlA7$Y, ChlA7$Z, color = spcol[as.numeric(ChlA7$Flow.rate)], pch = 20, xlab = "X Axis (m)", ylab = "Y Axis (m)", zlab = "Z Axis (m)", main = "Chlorophyll = 7.6 mg/L, colour = Flow Rate", cex.symbols = 0.5, angle = 55)
scatterplot3d(ChlA13$X, ChlA13$Y, ChlA13$Z, color = spcol[as.numeric(ChlA13$Flow.rate)], pch = 20, xlab = "X Axis (m)", ylab = "Y Axis (m)", zlab = "Z Axis (m)", main = "Chlorophyll = 13.5 mg/L, colour = Flow Rate", cex.symbols = 0.5, angle = 55)
scatterplot3d(ChlA19$X, ChlA19$Y, ChlA19$Z, color = spcol[as.numeric(ChlA19$Flow.rate)], pch = 20, xlab = "X Axis (m)", ylab = "Y Axis (m)", zlab = "Z Axis (m)", main = "Chlorophyll = 19 mg/L, colour = Flow Rate", cex.symbols = 0.5, angle = 55)
```
Now subsetting for Flow Rate
```{r}
## Will subset Totaldata into each set of variables and try plotting this again
levels(CC.TotalData$Flow.rate)
f0 <- CC.TotalData[CC.TotalData$Flow.rate == "0",] ##subsets CC.TotalData by Flow rate but only selects rows where flow rate = 0
head(f0)
tail(f0)
f.6 <- CC.TotalData[CC.TotalData$Flow.rate == "0.6",] ##subsets CC.TotalData by Flow rate but only selects rows where flow rate = 0.6
head(f.6)
tail(f.6)
f3<- CC.TotalData[CC.TotalData$Flow.rate == "3",] ##subsets CC.TotalData by Flow rate but only selects rows where flow rate = 3
head(f3)
tail(f3)
f5 <- CC.TotalData[CC.TotalData$Flow.rate == "5.9",] ##subsets CC.TotalData by Flow rate but only selects rows where flow rate = 5.9
head(f5)
tail(f5)
f8 <- CC.TotalData[CC.TotalData$Flow.rate == "8.9",] ##subsets CC.TotalData by Flow rate but only selects rows where flow rate =
head(f8)
tail(f8)
## need to make a list of colors for each level of Chlorophyll
levels(CC.TotalData$Chlorophyll)
spcol1 <- c("lightgoldenrod1", "khaki1", "darkolivegreen2", "greenyellow", "green2", "green4", "black")
## 3D scatterplot, colored by Flow.Rate BUT only points with 0 Chlorophyll
library(scatterplot3d)
scatterplot3d(f0$X, f0$Y, f0$Z, color = spcol1[as.numeric(f0$Chlorophyll)], pch = 20, xlab = "X Axis (m)", ylab = "Y Axis (m)", zlab = "Z Axis (m)", main = "Flow rate = 0 cm^3/s, colour = Chlorophyll", cex.symbols = 0.5, angle = 55)
scatterplot3d(f.6$X, f.6$Y, f.6$Z, color = spcol1[as.numeric(f.6$Chlorophyll)], pch = 20, xlab = "X Axis (m)", ylab = "Y Axis (m)", zlab = "Z Axis (m)", main = "Flow rate = 0.6 cm^3/s, colour = Chlorophyll", cex.symbols = 0.5, angle = 55)
scatterplot3d(f3$X, f3$Y, f3$Z, color = spcol1[as.numeric(f3$Chlorophyll)], pch = 20, xlab = "X Axis (m)", ylab = "Y Axis (m)", zlab = "Z Axis (m)", main = "Flow rate = 3 cm^3/s, colour = Chlorophyll", cex.symbols = 0.5, angle = 55)
scatterplot3d(f5$X, f5$Y, f5$Z, color = spcol1[as.numeric(f5$Chlorophyll)], pch = 20, xlab = "X Axis (m)", ylab = "Y Axis (m)", zlab = "Z Axis (m)", main = "Flow rate = 5.9 cm^3/s, colour = Chlorophyll", cex.symbols = 0.5, angle = 55)
scatterplot3d(f8$X, f8$Y, f8$Z, color = spcol1[as.numeric(f8$Chlorophyll)], pch = 20, xlab = "X Axis (m)", ylab = "Y Axis (m)", zlab = "Z Axis (m)", main = "Flow rate = 8.9 cm^3/s, colour = Chlorophyll", cex.symbols = 0.5, angle = 55)
```
Next is Gunao and Light
```{r}
head(CC.TotalData)
str(CC.TotalData)
## Guano
levels(CC.TotalData$Guano)
g0 <- CC.TotalData[CC.TotalData$Guano == "Absent",] ##subsets CC.TotalData by Guano but only selects rows where Guano = Absent
head(g0)
tail(g0)
g1<- CC.TotalData[CC.TotalData$Guano == "Present",] ##subsets CC.TotalData by Guano but only selects rows where Guano = Present
head(g1)
tail(g1)
##spcol = flow rate
##spcol1 = chlorophyll
## 3D scatterplot, colored by Flow.Rate BUT only points with 0 Chlorophyll
library(scatterplot3d)
scatterplot3d(g0$X, g0$Y, g0$Z, color = spcol1[as.numeric(g0$Chlorophyll)], pch = 20, xlab = "X Axis (m)", ylab = "Y Axis (m)", zlab = "Z Axis (m)", main = "Guano = Absent, colour = Chlorophyll", cex.symbols = 0.5, angle = 55)
scatterplot3d(g0$X, g0$Y, g0$Z, color = spcol[as.numeric(g0$Flow.rate)], pch = 20, xlab = "X Axis (m)", ylab = "Y Axis (m)", zlab = "Z Axis (m)", main = "Guano = Absent, colour = Flow Rate", cex.symbols = 0.5, angle = 55)
scatterplot3d(g1$X, g1$Y, g1$Z, color = spcol1[as.numeric(g1$Chlorophyll)], pch = 20, xlab = "X Axis (m)", ylab = "Y Axis (m)", zlab = "Z Axis (m)", main = "Guano = Present, colour = Chlorophyll", cex.symbols = 0.5, angle = 55)
scatterplot3d(g1$X, g1$Y, g1$Z, color = spcol[as.numeric(g1$Flow.rate)], pch = 20, xlab = "X Axis (m)", ylab = "Y Axis (m)", zlab = "Z Axis (m)", main = "Guano = Present, colour = Flow Rate", cex.symbols = 0.5, angle = 55)
###################################################################################################################################
## Light
levels(CC.TotalData$Light)
l0 <- CC.TotalData[CC.TotalData$Light == "Absent",] ##subsets CC.TotalData by Light but only selects rows where Light = Absent
head(l0)
tail(l0)
l1<- CC.TotalData[CC.TotalData$Light == "Present",] ##subsets CC.TotalData by Light but only selects rows where Light = Present
head(l1)
tail(l1)
##spcol = flow rate
##spcol1 = chlorophyll
## 3D scatterplot, colored by Flow.Rate BUT only points with 0 Chlorophyll
library(scatterplot3d)
scatterplot3d(l0$X, l0$Y, l0$Z, color = spcol1[as.numeric(l0$Chlorophyll)], pch = 20, xlab = "X Axis (m)", ylab = "Y Axis (m)", zlab = "Z Axis (m)", main = "Light = Absent, colour = Chlorophyll", cex.symbols = 0.5, angle = 55)
scatterplot3d(l0$X, l0$Y, l0$Z, color = spcol[as.numeric(l0$Flow.rate)], pch = 20, xlab = "X Axis (m)", ylab = "Y Axis (m)", zlab = "Z Axis (m)", main = "Light = Absent, colour = Flow Rate", cex.symbols = 0.5, angle = 55)
scatterplot3d(l1$X, l1$Y, l1$Z, color = spcol1[as.numeric(l1$Chlorophyll)], pch = 20, xlab = "X Axis (m)", ylab = "Y Axis (m)", zlab = "Z Axis (m)", main = "Light = Present, colour = Chlorophyll", cex.symbols = 0.5, angle = 55)
scatterplot3d(l1$X, l1$Y, l1$Z, color = spcol[as.numeric(l1$Flow.rate)], pch = 20, xlab = "X Axis (m)", ylab = "Y Axis (m)", zlab = "Z Axis (m)", main = "Light = Present, colour = Flow Rate", cex.symbols = 0.5, angle = 55)
##### testing alpha values
scatterplot3d(l1$X, l1$Y, l1$Z, color = spcol[as.numeric(l1$Flow.rate)], pch = 20, xlab = "X Axis (m)", ylab = "Y Axis (m)", zlab = "Z Axis (m)", main = "Light = Present, colour = Flow Rate", cex.symbols = 0.5, angle = 55)
```
Making plots for individual tracks and saving them
```{r}
## scatterplots for individuals (saves to working directory)
library(scatterplot3d)
for (i in 1:length(ind)){
##jpeg(filename=paste(ind[i], '.jpeg', sep = ''), width = 960, height = 960)
scatterplot3d(TotalData$X[TotalData$D_V_T==ind[i]], TotalData$Y[TotalData$D_V_T==ind[i]], TotalData$Z[TotalData$D_V_T==ind[i]], pch = 20, xlab = "X Axis (m)", ylab = "Y Axis (m)", zlab = "Z Axis (m)", main = ind[i], cex.symbols = 0.5)
##dev.off()
}
```
Now to plot each variable by density of points (heatmap style) in 2D and 3D
```{r}
#### plotting our data in 2D from a top down perspective
str(CC.TotalData)
library(ggplot2)
############################### Flow Rate ##########################################################
brks <- seq(-2,2,0.01)
lbls <- brks[-0.01]-0.005
gg.df <- aggregate(Z ~ cut(X, brks, lbls) + cut(Y, brks, lbls) + CC.TotalData$Flow.rate, CC.TotalData, FUN=mean)
names(gg.df)[1:3] <- c("X", "Y", "Flow.rate")
gg.df$X <- as.numeric(as.character(gg.df$X))
gg.df$Y <- as.numeric(as.character(gg.df$Y))
p <- ggplot(gg.df, aes(x=X, y=Y, alpha=Z, fill=Flow.rate)) + geom_raster() + coord_fixed()
p + scale_fill_brewer(palette = "YlOrRd")
############################### Chlorophyll ##########################################################
brks <- seq(-2,2,0.01)
lbls <- brks[-0.01]-0.005
gg.df <- aggregate(Z ~ cut(X, brks, lbls) + cut(Y, brks, lbls) + CC.TotalData$Chlorophyll, CC.TotalData, FUN=mean)
names(gg.df)[1:3] <- c("X", "Y", "Chlorophyll")
gg.df$X <- as.numeric(as.character(gg.df$X))
gg.df$Y <- as.numeric(as.character(gg.df$Y))
p <- ggplot(gg.df, aes(x=X, y=Y, alpha=Z, fill=Chlorophyll)) + geom_raster() + coord_fixed()
p + scale_fill_brewer(palette = "YlGn")
############################### Guano ##########################################################
brks <- seq(-2,2,0.01)
lbls <- brks[-0.01]-0.005
gg.df <- aggregate(Z ~ cut(X, brks, lbls) + cut(Y, brks, lbls) + CC.TotalData$Guano, CC.TotalData, FUN=mean)
names(gg.df)[1:3] <- c("X", "Y", "Guano")
gg.df$X <- as.numeric(as.character(gg.df$X))
gg.df$Y <- as.numeric(as.character(gg.df$Y))
p <- ggplot(gg.df, aes(x=X, y=Y, alpha=Z, fill=Guano)) + geom_raster() + coord_fixed()
p + scale_fill_brewer(palette = "Reds")
############################## Light ##########################################################
brks <- seq(-2,2,0.01)
lbls <- brks[-0.01]-0.005
gg.df <- aggregate(Z ~ cut(X, brks, lbls) + cut(Y, brks, lbls) + CC.TotalData$Light, CC.TotalData, FUN=mean)
names(gg.df)[1:3] <- c("X", "Y", "Light")
gg.df$X <- as.numeric(as.character(gg.df$X))
gg.df$Y <- as.numeric(as.character(gg.df$Y))
p <- ggplot(gg.df, aes(x=X, y=Y, alpha=Z, fill=Light)) + geom_raster() + coord_fixed()
p + scale_fill_brewer(palette = "Greys")
```
```{r}
#### plotting our data in 2D from a side on perspective
############################### Flow Rate ##########################################################
brks <- seq(-2,2,0.01)
lbls <- brks[-0.01]-0.005
gg.df <- aggregate(X ~ cut(Y, brks, lbls) + cut(Z, brks, lbls) + CC.TotalData$Flow.rate, CC.TotalData, FUN=mean)
names(gg.df)[1:3] <- c("Y", "Z", "Flow.rate")
gg.df$Y <- as.numeric(as.character(gg.df$Y))
gg.df$Z <- as.numeric(as.character(gg.df$Z))
p <- ggplot(gg.df, aes(x=Y, y=Z, alpha=X, fill=Flow.rate)) + geom_raster() + coord_fixed()
p + scale_fill_brewer(palette = "YlOrRd")
############################### Chlorophyll ##########################################################
brks <- seq(-2,2,0.01)
lbls <- brks[-0.01]-0.005
gg.df <- aggregate(X ~ cut(Y, brks, lbls) + cut(Z, brks, lbls) + CC.TotalData$Chlorophyll, CC.TotalData, FUN=mean)
names(gg.df)[1:3] <- c("Y", "Z", "Chlorophyll")
gg.df$Y <- as.numeric(as.character(gg.df$Y))
gg.df$Z <- as.numeric(as.character(gg.df$Z))
p <- ggplot(gg.df, aes(x=Y, y=Z, alpha=X, fill=Chlorophyll)) + geom_raster() + coord_fixed()
p + scale_fill_brewer(palette = "YlGn")
############################### Guano ##########################################################
brks <- seq(-2,2,0.01)
lbls <- brks[-0.01]-0.005
gg.df <- aggregate(X ~ cut(Y, brks, lbls) + cut(Z, brks, lbls) + CC.TotalData$Guano, CC.TotalData, FUN=mean)
names(gg.df)[1:3] <- c("Y", "Z", "Guano")
gg.df$Y <- as.numeric(as.character(gg.df$Y))
gg.df$Z <- as.numeric(as.character(gg.df$Z))
p <- ggplot(gg.df, aes(x=Y, y=Z, alpha=X, fill=Guano)) + geom_raster() + coord_fixed()
p + scale_fill_brewer(palette = "Reds")
############################## Light ##########################################################
brks <- seq(-2,2,0.01)
lbls <- brks[-0.01]-0.005
gg.df <- aggregate(X ~ cut(Y, brks, lbls) + cut(Z, brks, lbls) + CC.TotalData$Light, CC.TotalData, FUN=mean)
names(gg.df)[1:3] <- c("Y", "Z", "Light")
gg.df$Y <- as.numeric(as.character(gg.df$Y))
gg.df$Z <- as.numeric(as.character(gg.df$Z))
p <- ggplot(gg.df, aes(x=Y, y=Z, alpha=X, fill=Light)) + geom_raster() + coord_fixed()
p + scale_fill_brewer(palette = "Greys")
```
```{r}
### interpolate the data so it's smoother
library(fields)
## E.g. but with variables instead
s = smooth.2d(CC.TotalData$Z, x=cbind(CC.TotalData$X, CC.TotalData$Y), theta=0.3)
image.plot(s, axes=TRUE, xlab = "X", ylab = "Y", legend.lab = "Z", legend.width = .5, legend.line = 3)
## need to find how to do a stacked heatmap like what Kuvvat showed in MatLab
```
Finally save the image
```{r}
save.image("~/Post-doc/Data/Total Merged Data File (April 5 2022).RData")
```