-
Notifications
You must be signed in to change notification settings - Fork 0
/
3D_plots2.R
620 lines (470 loc) · 21.6 KB
/
3D_plots2.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
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
library(sf)
library(plotly)
library(raster)
library(rjson)
library(RColorBrewer)
library(lubridate)
library(stringr)
library(dplyr)
library(viridis)
#### Read in SHP, convert to MULTISTRING, extract geometry ####
temp = list.files(path = "SHP/",pattern="*.shp") #list of all SHP files
x <- seq(1, length(temp),1) #sequence as long as number of CSVs, used for loop functions
shapes <- list() #empty list to fill
for (i in x){
thing <- read_sf(paste0("SHP/",temp[[i]])) #reads in each CSV from temp list
shapes[[i]] <- thing #adds data to data list
}
for (i in x){
shapes[[i]] <- st_cast(shapes[[i]], "MULTILINESTRING") #converts polygons to multilinestrings
}
#### Read in CSV for Dates ####
temp = list.files(path = "CSV/",pattern="*.csv") #list of all CSV files
csv <- list() #empty list to fill
alldates <- list()
for (i in x){
thing <- read.csv(file = paste0("CSV/",temp[[i]])) #reads in each CSV from temp list
csv[[i]] <- thing #adds data to dates list
}
sf <- stamp("Jan 13", order = "Obd") #create date stamp for labeling
for (i in x){
csv[[i]]$Labels <- csv[[i]]$Date #create new labal column & fill with dates
csv[[i]]$Labels <- sf(mdy(csv[[i]]$Labels)) #updates label dates with stamp dates
alldates[[i]] <- csv[[i]]$Labels[1]
}
for (i in x){
csv[[i]]$geometry <- csv[[i]]$geometry
}
encampments <- c() #empty list to fill
for (i in x){ #cylces through each date
y <- seq(1, length(csv[[i]]$Camp),1) #gets number of camps for the date
for (j in y){ #runs through each camp for the date
if (csv[[i]]$Camp[j] %in% encampments) { #if the camp is already in encampments skip
} else {
encampments <- append(encampments, csv[[i]]$Camp[j]) #add camp to encampments if it isn't already
}
}
}
#dates <- c() #empty list to fill
#for (i in x){ #cyles through each file
# y <- seq(1, length(csv[[i]]$Date),1) #list of date entries for file
# for (j in y){ #cylces through date entries
# if (csv[[i]]$Date[j] %in% dates) { #if the date is already in dates skip
# dates <- dates
# } else { #add date to dates if it isn't on the list
#
# dates <- append(dates,csv[[i]]$Date[j])
# }
# }
#}
counts <- list() #empty list to fill with camp/tent dataframes
for (i in x){ #loops through each CSV
c <- c(csv[[i]]$Camp) #creates list of camps
t <- c(csv[[i]]$Tents) #creates list of tent counts
counts[[i]] <- data.frame(c,t) #turns camps/tents into data frame and adds to list
}
encampments.df <- as.data.frame(encampments) #sets top row of df as encampment names
for (i in x) { #loops through each CSV
z <- seq(1, length(encampments), 1) #makes sequence equal to number of encampments
q <- c() #empty list to fill with tent counts per encampment
for (j in z){ #loops through each encampment name
if(encampments[[j]] %in% counts[[i]]$c){ #checks if encampment is in current data set
l <- which(counts[[i]] == encampments[[j]], arr.ind = T) #gives df row & column for encampment
q <- append(q, counts[[i]]$t[l[1]]) #finds tent count associated with encampment and adds it df
}else{
q <- append(q, NA) #if the encampment isn't in the data set to 0
}
}
encampments.df <- cbind(encampments.df, q) #adds all encampment tent counts for date to df
}
row.names(encampments.df) <- encampments #name rows encampments
encampments.df[1] <- NULL #remove excess 'encampments column
colnames(encampments.df) <- alldates #name columns from dates
encampments.df <- t(encampments.df) #transpose df, turns it into a matrix
encampments.df <- as.data.frame(encampments.df) #convert back to final df
pal <- rainbow(length(encampments))
large.tent <- max(encampments.df, na.rm = T)
pal2 <- magma(large.tent)
encampments.df <- cbind(encampments.df, "Total Tents" = rowSums(encampments.df, na.rm = T)) #add total tents count summed row
#### Loop to create Geo Ref Dataframe ####
encampments.df.geo <- encampments.df
encampments.df.geo <- encampments.df.geo[,-ncol(encampments.df)]
for ( i in seq(1, nrow(encampments.df.geo),1)){
for ( j in seq(1, ncol(encampments.df.geo),1)){
if (is.na(encampments.df[alldates[[i]],encampments[[j]]])){
} else {
encampments.df.geo[i,j] <- paste0("shapes[[",i,"]]$geometry[[",match(encampments[[j]], csv[[i]]$Camp),"]][[1]]")
}
}
}
summary(encampments.df.geo[1,1])
#### Dates 3D Map ####
fig2 <- plot_ly()
for ( i in seq(1, nrow(encampments.df.geo),1)){
date <- as.list(encampments.df.geo[i,])
for (j in seq(1, ncol(encampments.df.geo),1)){
first.camp <- which(!is.na(encampments.df.geo[,j])) %>%
min(.)
camp <- as.data.frame(eval(parse(text = encampments.df.geo[i,j])))
group <- paste(encampments[[j]])
camp.name <- c(paste0("Camp: ", encampments[[j]], "<br>")) #name camp
date <- c(paste0("Date: ", alldates[[i]])) #name date
hover <- c(paste0(camp.name,date))
if (!is.na(encampments.df[i,j])){
colors <- as.data.frame(rep(encampments.df[i,j], nrow(camp)))
camp <- cbind(camp,colors)
fig2 <- fig2 %>%add_trace(date, x = camp$V1,
y = camp$V2,
z = alldates[[i]],
type = 'scatter3d',
mode = 'lines',
line = list(width = 7,
color = camp[,4],
cmin = 2,
cmax = large.tent,
colorscale = 'Electric',
coloraxis = 'coloraxis'),
showlegend = F, #show legend (for camp group)
visible = T, #make visible
hovertext = hover, #sets hover text to created array
hoverinfo = "text",
opacity = .8, #80% opacity
name = paste0(group), #names it the encampment
legendgroup = group)
} else {
fig2 <- fig2 %>%add_trace(date, x = camp$V1,
y = camp$V2,
z = alldates[[i]],
type = 'scatter3d',
mode = 'lines',
line = list(width = 7,
color = "red"),
showlegend = F, #show legend (for camp group)
visible = T, #make visible
hovertext = hover, #sets hover text to created array
hoverinfo = "text",
opacity = .8, #80% opacity
name = paste0(group), #names it the encampment
legendgroup = group)
}
}
}
steps <- list()
rep <- rep(FALSE, nrow(encampments.df.geo) * ncol(encampments.df.geo))
final.end <- length(rep)
last.camp <- 1
for ( i in seq(1, nrow(encampments.df.geo),1)){
step <- list(args = list(list(visible = rep), list(showlegend = rep)),
method = 'update',
label = print(alldates[[i]])) #labels each map with its associated date
last.camp2 <- which(!is.na(encampments.df.geo[i,])) %>%
max(.)
if (last.camp < last.camp2){
last.camp <- last.camp2
}
leg.start <- ((i - 1) * ncol(encampments.df.geo)) + 1
leg.end <- leg.start + (last.camp - 1)
end <- ncol(encampments.df.geo) * i
step$args[[1]]$visible[1 : end] = TRUE
step$args[[1]]$showlegend[1 : final.end] = FALSE
step$args[[1]]$showlegend[leg.start : leg.end] = TRUE
step$args[[2]]$showlegend[leg.start : leg.end] = TRUE
steps[[i]] = step
}
buttons <- list()
for ( i in seq(1, nrow(encampments.df.geo),1)){
button <- list(args = list(list(visible = rep), list(showlegend = rep)), method = 'update', label = print(alldates[[i]])) #labels each map with its associated date
end <- ncol(encampments.df.geo) * i
start <- (ncol(encampments.df.geo) * (i - 1)) + 1
#turns on the single map selected, set to true, all other maps left invisible
button$args[[1]]$visible[start : end] = TRUE
button$args[[1]]$showlegend[start : end] = TRUE
button$args[[2]]$showlegend[start : end] = TRUE
buttons[[i]] = button
}
xrange <- list(-12461000, -12453000)
yrange <- list(4974000, 4979000)
zrange <- list(0, length(alldates))
fig2 <- fig2 %>% layout(
scene = list(
camera = list(
eye = list(
x = 0,
y = -1.25,
z = .5
)
),
bgcolor = 'rgb(220,220,220)',
aspectmode = 'manual', #sets aspect ratio
aspectratio = list(
x= 1,
y =.76,
z = .1
),
xaxis = list(
autorange = FALSE,
range = xrange,
showgrid = FALSE,
title = list(
text = "East - West"
),
showticklabels = FALSE,
showbackground = TRUE,
backgroundcolor = 'rgb(240,240,240)'
),
yaxis = list(
autorange = FALSE,
range = yrange,
showgrid = FALSE,
title = list(
text = "North- South"
),
showticklabels = FALSE,
showbackground = TRUE,
backgroundcolor = 'rgb(240,240,240)'
),
zaxis = list(
autorange = FALSE,
range = zrange,
showgrid = FALSE,
title = list(
text = "Time"
),
type = 'category',
categoryorder = "array",
categoryarray = alldates,
showticklabels = FALSE,
showbackground = TRUE,
backgroundcolor = 'rgb(240,240,240)'
)),
legend = list(
itemsizeing = "constant",
itemwidth = 60,
itemclick = "toggleothers",
itemdoubleclick = 'toggle',
title = list(
text = "Camps"
),
bgcolor = 'rgb(220,220,220)',
bordercolor = 'rgb(0,0,0)',
borderwidth = 1,
xanchor = 'right',
x = -0.02
),
title = list(
text = paste0("SLC Homeless Encampments: ", alldates[[1]], " - ", alldates[[length(alldates)]])
),
sliders = list(list(active = length(alldates) - 1, #turns sliders on
bgcolor = "#838289", #stylize sliders
bordercolor = "#33303f", #stylize sliders
activebgcolor = "#292929", #stylize sliders
pad = list( #stylize sliders
b = 10
),
currentvalue = list(
prefix = "Feb 05 - ",
visible = T, #call it date and pull label (ie date)
offset = 20,
font = list(
size = 15,
color = "#33303f"
)
),
font = list(
color = "#33303f"
),
steps = steps, #use steps data
transition = list(
duration = 0
)),
list(active = length(alldates) - 1,
bgcolor = "#838289", #stylize sliders
bordercolor = "#33303f", #stylize sliders
activebgcolor = "#292929", #stylize sliders
pad = list( #stylize sliders
b = 10
),
currentvalue = list(
prefix = "Date: ",
visible = T, #use current value feature
offset = 20,
font = list(
size = 15,
color = "#33303f"
)
),
font = list(
color = "#33303f"
),
steps = buttons, #use steps data
transition = list(
duration = 0
),
yanchor = "top",
y = -.2)
),
coloraxis = list(
cmax = large.tent,
cmin = 2,
colorscale = 'Electric'
),
margin = list(r = 20))
fig
#### Camps 3D Map ####
fig <- plot_ly()
for ( i in seq(1, nrow(encampments.df.geo),1)){
for (j in seq(1, ncol(encampments.df.geo),1)){
first.camp <- which(!is.na(encampments.df.geo[,j])) %>%
min(.)
group <- paste(encampments[[j]])
camp.name <- c(paste0("Camp: ", encampments[[j]], "<br>")) #name camp
tents <- c(paste0("Tents: ", encampments.df[i,j])) #name date
hover <- c(paste0(camp.name,tents))
if (is.na(encampments.df.geo[i,j])){
camp <- as.data.frame(eval(parse(text = encampments.df.geo[first.camp,j])))
colors <- as.data.frame(rep(encampments.df[i,j], nrow(camp)))
camp <- cbind(camp,colors)
fig <- fig %>%add_trace(date, x = camp$V1,
y = camp$V2,
z = alldates[[i]],
type = 'scatter3d',
mode = 'lines',
line = list(width = 7,
color = camp[,4],
cmin = 2,
cmax = large.tent,
colorscale = 'Electric',
coloraxis = 'coloraxis'),
showlegend = F, #show legend (for camp group)
visible = F, #make visible
hovertext = hover, #sets hover text to created array
hoverinfo = "text",
opacity = .8, #80% opacity
name = paste0(encampments[[j]]), #names it the encampment
legendgroup = group)
} else if ( i == first.camp ){
camp <- as.data.frame(eval(parse(text = encampments.df.geo[i,j])))
colors <- as.data.frame(rep(encampments.df[i,j], nrow(camp)))
camp <- cbind(camp,colors)
fig <- fig %>%add_trace(date, x = camp$V1,
y = camp$V2,
z = alldates[[i]],
type = 'scatter3d',
mode = 'lines',
line = list(width = 7,
color = camp[,4],
cmin = 2,
cmax = large.tent,
colorscale = 'Electric',
coloraxis = 'coloraxis'),
showlegend = T, #show legend (for camp group)
visible = T, #make visible
hovertext = hover, #sets hover text to created array
hoverinfo = "text",
opacity = .8, #80% opacity
name = paste0(encampments[[j]]), #names it the encampment
legendgroup = group)
} else {
camp <- as.data.frame(eval(parse(text = encampments.df.geo[i,j])))
fig <- fig %>%add_trace(date, x = camp$V1,
y = camp$V2,
z = alldates[[i]],
type = 'scatter3d',
mode = 'lines',
line = list(width = 7,
color = pal2[encampments.df[i,j]]),
showlegend = F, #show legend (for camp group)
visible = T, #make visible
hovertext = hover, #sets hover text to created array
hoverinfo = "text",
opacity = .8, #80% opacity
name = paste0(encampments[[j]]), #names it the encampment
legendgroup = group)
}
}
}
buttons <- list()
for ( i in seq(1, ncol(encampments.df.geo),1)){
button <- list(args = list('visible', rep(FALSE, nrow(encampments.df.geo) * ncol(encampments.df.geo))), #creates a list for step values set to FALSE for each map
method = 'restyle',
label = print(encampments[[i]])) #labels each map with its associated date
last.camp <- which(!is.na(encampments.df.geo[,i])) %>%
max(.)
onlist <- list()
for ( j in seq(1, last.camp, 1)) {
if (is.na(encampments.df.geo[j,i])){
on <- NA
} else {
on <- i + (ncol(encampments.df.geo) * (j - 1))
}
onlist[[j]] <- on
}
for ( k in seq(1, last.camp, 1)){
if (is.na(onlist[[k]])){
button$args[[2]][onlist[[k]]] = FALSE
} else {
button$args[[2]][onlist[[k]]] = TRUE
}
}
buttons[[i]] = button
}
fig <- fig %>% layout(scene = list(
bgcolor = 'rgb(220,220,220)',
aspectmode = 'data',
xaxis = list(
autorange = T,
showgrid = FALSE,
title = list(
text = "East - West"
),
showticklabels = FALSE,
showbackground = TRUE,
backgroundcolor = 'rgb(240,240,240)'
),
yaxis = list(
autorange = T,
showgrid = FALSE,
title = list(
text = "North- South"
),
showticklabels = FALSE,
showbackground = TRUE,
backgroundcolor = 'rgb(240,240,240)'
),
zaxis = list(
autorange = T,
showgrid = FALSE,
title = list(
text = ""
),
type = 'category',
categoryorder = "array",
categoryarray = alldates,
showticklabels = TRUE,
showbackground = TRUE,
backgroundcolor = 'rgb(240,240,240)'
)),
legend = list(
itemsizeing = "constant",
itemwidth = 60,
itemclick = "toggleothers",
title = list(
text = "Camps"
),
bgcolor = 'rgb(220,220,220)',
bordercolor = 'rgb(0,0,0)',
borderwidth = 1
),
title = list(
text = "SLC Homeless Encampments"
),
coloraxis = list(
cmax = large.tent,
cmin = 2,
colorscale = 'Electric',
colorbar = list(
xanchor = 'right',
x = -0.02
)
))
fig