forked from ameliaritger/mbon-shiny-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.R
679 lines (600 loc) · 36.2 KB
/
app.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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
# community composition plot - genus within phylum?
# community composition plot - pick a phylum then pick a location
# fix neighbor plot
#attach packages
library(tidyverse)
library(janitor)
library(shiny)
library(shinythemes)
library(shinyWidgets)
library(shinyLP)
library(sf)
library(tmap)
library(vegan)
library(gt)
library(leaflet)
library(collapsibleTree)
library(shinycssloaders)
library(reshape2)
library(plotly)
library(networkD3)
#add functionality to publish app
library(rsconnect)
library(BiocManager)
options(repos = BiocManager::repositories())
#until janitor() package issues are resolve, download older version of it!
#require(devtools)
#install_version("janitor", version = "1.2.1", repos = "http://cran.us.r-project.org")
#until sf() package issues are resolve, download older versions of it/dependencies!
#install_version("sf", version = "0.8-1", repos = "http://cran.us.r-project.org")
#install_version("lwgeom", version = "0.2-1", repos = "http://cran.us.r-project.org")
#install_version("tmap", version = "2.3-2", repos = "http://cran.us.r-project.org")
#install_version("stars", version = "0.4-0", repos = "http://cran.us.r-project.org"))
####################################################################
## Read in data
reef <- read_csv("MBONReef_Histogram.csv")
## Tidy up the data
reef_tidy <- reef %>%
clean_names() %>% #standardize names
group_by(location) %>% #group by location to get average lat/long values for each location
mutate(latitude=head(lat_start,1), longitude=head(long_start,1)) %>% #get average lat/long values (because that's how that works...)
ungroup() %>% #really important, we don't want to confuse R!
pivot_longer("annelida_cirriformia_luxuriosa":"substrate_amphipod_tube_complex") %>% #make long form
separate(name, into="phylum", sep="_", remove=FALSE) %>% #Add column for phylum name
mutate(vectorized_name=str_split(name, pattern="_")) %>% #In case this is useful...
filter(!phylum=="no") %>% #remove values related to data collection issue
filter(!phylum=="substrate") #remove substrate values
#Because it's faster to do it outside tidyverse
reef_tidy$binary <- ifelse(reef_tidy$value>0, 1, 0) #Add presence/absence column
reef_tidy$species <- gsub("^[^_]*_","",reef_tidy$name, perl=TRUE) #Add column for species name
reef_tidy$longitude <- ifelse(reef_tidy$longitude<0, reef_tidy$longitude, -reef_tidy$longitude) #because all longitude values in this region should be negative - looking at you, Rodes...
#Do some more tidying
reef_tidy <- reef_tidy %>%
mutate(species = str_replace(species, pattern="tube worm", "tubeworm")) %>% #replace "tube worm" with "tubeworm" for later grouping
separate(species, into="genus", sep="_", remove=FALSE) %>% #Add column for genus name
mutate(species=str_replace_all(species, "_", " "),
species=str_to_sentence(species)) %>% #Make species names actually look like species names
filter(!str_detect(species, pattern="dead")) #Remove instances where organism is dead
#Now time for some massive if, else statements to group organisms not identified to species
reef_tidy <- reef_tidy %>%
mutate(grouped_species = ifelse(str_detect(species, pattern = " worm"), "Other worms", ifelse(str_detect(species, pattern = "Filamentous"), "Other Algaes", ifelse(str_detect(species, pattern = "turf"), "Other Algaes", ifelse(str_detect(species, pattern = "blade"), "Other Algaes", ifelse(str_detect(species, pattern = "tunicate"), "Other Tunicates", ifelse(str_detect(species, pattern = "anemone"), "Other anemones", ifelse(str_detect(species, pattern = "bryozoan"), "Other bryozoans", ifelse(str_detect(species, pattern = "White fan"), "Other bryozoans", ifelse(str_detect(species, pattern = "sponge"), "Other Sponges", ifelse(str_detect(species, pattern = "Orange encrusting"), "Other Sponges", ifelse(str_detect(species, pattern = "Haliclona sp"), "Other Sponges", ifelse(str_detect(species, pattern = "zigzag"), "Other Hydroids", species))))))))))))) %>%
mutate(grouped_genus = ifelse(str_detect(grouped_species, pattern = "Other"), grouped_species, ifelse(str_detect(grouped_species, pattern = "orange"), grouped_species, ifelse(str_detect(grouped_species, pattern = "White"), grouped_species, ifelse(str_detect(grouped_species, pattern = "Encrusting"), "Other Algaes", ifelse(str_detect(grouped_species, pattern = "zigzag"), grouped_species, ifelse(str_detect(grouped_species, pattern = "solitary"), grouped_species, ifelse(str_detect(grouped_species, pattern = "sectioned"), grouped_species, genus)))))))) %>% #do the same for genus
mutate(grouped_genus = str_to_title(grouped_genus)) %>% #capitalize genus name
mutate(phylum = str_to_title(phylum)) %>% #capitalize phylum name
mutate(grouped_species = ifelse(str_detect(grouped_species, pattern = "Sectioned"), "Sectioned worms", ifelse(str_detect(grouped_species, pattern = "Solitary"), "Solitary worms", ifelse(str_detect(grouped_species, pattern = "Celleporella"), "Celleporella 1", grouped_species)))) %>%
mutate(grouped_genus = ifelse(str_detect(grouped_genus, pattern = "Sectioned"), "Other Worms", ifelse(str_detect(grouped_genus, pattern = "Solitary"), "Other Worms", ifelse(str_detect(grouped_genus, pattern = "Barnacles"), "Other Barnacles", ifelse(str_detect(grouped_genus, pattern = "Zigzag"), "Other Hydroids", ifelse(str_detect(grouped_genus, pattern = "Zoanthid"), "Other Zoanthids", grouped_genus))))))
#Create separate dataframe of just latitude, longitude, and locations (use for later plotting species diversity/richness at each location)
reef_location <- reef_tidy %>%
distinct(location, latitude, longitude)
## Find species diversity/richness for each site
#Prep data
reef_vegan <- reef_tidy %>% #named so because of the vegan package!
group_by(location,grouped_species) %>% #group by location, then lat/long
summarize(mean_count = mean(value)) %>% #get the mean count
select(location, grouped_species, mean_count) %>%
ungroup()
#Calculate species diversity and richness for each site
reef_vegan_subset <- reef_vegan %>%
pivot_wider(names_from=grouped_species, values_from=mean_count) %>%
select(`Abietinaria spp`:`Zonaria farlowii`)
Diversity <- diversity(reef_vegan_subset, index="shannon")
Richness <- specnumber(reef_vegan_subset)
#Combine all of this information - location, lat/long, diversity/richness
reef_vegan <- reef_location %>%
add_column(Diversity, Richness)
#Create color palette for each phylum
pal <- c(
"Annelida" = "#D2691E",
"Arthropoda" = "#CDCDB4",
"Chlorophyta" = "#A2CD5A",
"Chordata" = "#FFB90F",
"Cnidaria" = "#B4CDCD",
"Echinodermata" = "#FF6347",
"Ectoprocta" = "#FF8C00",
"Fish" = "#CD3700",
"Heterokontophyta" = "#8B814C",
"Mollusca" = "#708090",
"Phoronida" = "#FAFAD2",
"Porifera" = "#EEDC82",
"Rhodophyta" = "#DB7093"
)
#Generate list of MPA sites
mpa_sites <- c("Anacapa Landing", "Cathedral Cove", "Gull Island", "Isla Vista", "Naples")
#"Arroyo Quemado", "Carpinteria", "West End Cat Rock"
####################################################################
#Create user interface
ui <- navbarPage("Marine Biodiversity Observation Network",
theme = shinytheme("simplex"),
## TAB
tabPanel("About the app",
fluidRow(column(12,
jumbotron("Welcome!", "This app allows users to visualize benthic survey data collected in kelp forest communities in the Santa Barbara Channel (SBC).",button=FALSE)),
br(),
br(),
),
fluidRow(column(12, align="center",
#imageOutput('home_image',inline = TRUE),
h4(HTML('Want to learn more about how these data were collected? Check out the <a href="https://portal.edirepository.org/nis/mapbrowse?scope=edi&identifier=484" target="_blank">data repository</a>.'))
)),
br(),
br(),
HTML('<center><img src="mbon.png" width="500"></center>'),
br(),
br(),
br(),
fluidRow(column(12, align="center",
#imageOutput('home_image',inline = TRUE),
h4(HTML('<a href="https://ameliaritger.shinyapps.io/mbon-shiny-app/" target="_blank">View the full screen version of this app.</a>'))
)),
fluidRow(column(12, align="center",
h5(HTML('Code and data used to create this Shiny app are available on <a href="https://github.com/ameliaritger/mbon-shiny-app" target="_blank">Github</a>.'))
)),
fluidRow(column(12, align="center",
h6(HTML('Found an issue with the app? Have a feature you would like to request? Reach out to the <a href="https://ameliaritger.netlify.com" target="_blank">app creator</a>!'))
))
),
## TAB
tabPanel("About the critters",
h1("Not familiar with the critters of this dataset? Look no further!"),
p(em("Please be patient, this page may take a few seconds to load.")),
sidebarLayout(
sidebarPanel("",
selectInput(inputId="phylumSelectComboTree",
label="Pick a phylum!",
choices=unique(reef_tidy$phylum)
),
h5(p("Curious what an organism in that phylum looks like?")),
tags$head(tags$style(
type="text/css",
"#phylum_image img {max-width: 100%; width: 100%; height: auto}" #make image reactive to page size
)),
imageOutput("phylum_image"),
selectizeInput("searchaphylum",
label = "Want to learn more about an organism?",
choices = sort(c(unique(reef_tidy$grouped_genus), unique(reef_tidy$grouped_species))),
multiple = FALSE,
options = list(placeholder='Enter genus or species name',
onInitialize = I('function() { this.setValue(""); }')
)
),
uiOutput("url", style = "font-size:20px; text-align:center")
),
mainPanel(h3(p("Hierarchical tree of the species found in this dataset")),
collapsibleTreeOutput('species_tree', height='600px') %>%
withSpinner(color = "#008b8b"),
br(),
br(),
h6(HTML('With inspiration from the Biodiversity in National Parks <a href="https://abenedetti.shinyapps.io/bioNPS/" target="_blank">Shiny app</a>.'))
)
)
),
## TAB
tabPanel("Diversity",
h1("Species diversity and richness across the SBC"),
p(em("Calculated from mean count values for each species.")),
sidebarLayout(
sidebarPanel("",
radioButtons(inputId="pickanindex",
label="Pick an output!",
choices=c("Richness","Diversity")
),
checkboxGroupInput("mpaselect_diversity", label = "", choices= c("Display Marine Protected Areas (MPAs)"="mpa", "Display unprotected areas"="unprotected"), selected=c("mpa", "unprotected")),
br(),
plotOutput(outputId="plot_index"),
br(),
h5(p(em("How is each term calculated?"))),
h6(p(strong("Richness:"))),
h6(p("The number of species within a community.")),
h6(p(strong("Diversity:"))),
h6(p("The number of species within a community (richness) and the relative abundance of each species (evenness).", em(HTML('Here, we used the <a href="https://en.wikipedia.org/wiki/Diversity_index#Shannon_index" target="_blank">Shannon-Wiener Index</a>.'))))
),
mainPanel(h4(p("")),
leafletOutput("map_index"),
)
)
),
## TAB
tabPanel("Abundance",
h1("Mean abundance of marine organisms across the SBC"),
p(em("Calculated from mean count values for each organism.")),
sidebarLayout(
sidebarPanel("",
selectizeInput(inputId="mapitabundance",
label = "Enter a phylum or species name!",
choices = sort(c(unique(reef_tidy$grouped_species), unique(reef_tidy$phylum))),
multiple = FALSE,
selected = 'Annelida'),
checkboxGroupInput("mpaselect_abundance", label = "", choices= c("Display Marine Protected Areas (MPAs)"="mpa", "Display unprotected areas"="unprotected"), selected=c("mpa", "unprotected")),
br(),
plotOutput(outputId="plot_abundance"),
),
mainPanel(h4(p("")),
leafletOutput("map_abundance")
)
)
),
## TAB
tabPanel("Community",
h1("Community composition at each site"),
p("Compare ecological communities on vertical and horizontal surfaces along the reef.", em("Calculated from presence (yes or no) in replicate quadrats at each of the 22 sites in the SBC.")),
sidebarLayout(
sidebarPanel("",
selectInput(inputId="locationselect",
label="Pick a location!",
choices=sort(unique(reef_tidy$location))
),
radioButtons(inputId = "orientationselect",
label = "Pick an orientation!",
choices = c("All"="l", "Vertical"="vertical", "Horizontal"="horizontal")
),
h6(p(em("Note: not all locations have both vertical and horizontal orientations."))),
fluidRow(column(10, align="left",
checkboxInput("pickasankey", label = "Display Sankey diagram (interactive)", value = FALSE)),
column(10, align="left",
conditionalPanel(condition = "input.pickasankey == '1'",
numericInput('sankeynumber', 'Pick the number of top phyla to display!', 1, min = 1, max = 5))),
column(12, align="left",
conditionalPanel(condition = "input.pickasankey == '1'",
h6(p(em("The width of the bands in a", HTML('<a href="https://en.wikipedia.org/wiki/Sankey_diagram" target="_blank">Sankey diagram</a>'), "are proportional to abundance.")))))
),
br(),
h5(p("Curious what a quadrat from the location looks like?")),
tags$head(tags$style(
type="text/css",
"#location_image img {max-width: 100%; width: 100%; height: auto}" #make image reactive to page size
)),
imageOutput("location_image")
),
mainPanel("",
plotOutput(outputId="plot_community"),
br(),
conditionalPanel(
condition = "input.pickasankey == '1'",
sankeyNetworkOutput("sankey_plot"))
)
)
),
## TAB
tabPanel("Neighbors",
h1("Will you be my neighbor? Evaluating how often organisms are found together."),
p("Compare organismal co-occurrence across the SBC.", em("Calculated from presence (yes or no) in replicate quadrats at all 22 sites in the SBC.")),
sidebarLayout(
sidebarPanel("",
selectInput(inputId="pickaphylum",
label="Pick a phylum!",
choices=unique(reef_tidy$phylum)
),
pickerInput(inputId="coocurring",
label="Pick some neighbors!",
choices=unique(reef_tidy$phylum),
selected="Annelida",
options = list(`actions-box`=TRUE,
`selected-text-format` = "count > 3"),
multiple = TRUE),
pickerInput(inputId="pickalocation",
label="Pick one (or more) locations!",
choices=unique(reef_tidy$location),
selected=unique(reef_tidy$location),
options = list(`actions-box`=TRUE,
`selected-text-format` = "count > 3"),
multiple = TRUE),
fluidRow(column(10, align="left",
checkboxInput("pickaplot", label = "Display heat map (interactive)", value = FALSE))),
#p(strong("ADD ~Or, pick a genus~ HERE?")),
br(),
#plotlyOutput(outputId="plot_heatmap"),
br(),
h5(p(em("What is the difference between the plot and the table?"))),
p(strong("The plot"), "displays the unique number of quadrats containing the focal organism and", em("each"), "neighbor organism.", strong("The table"), "displays the unique number of quadrats containing the focal organism and", em("all"), "neighbor organisms."),
p("Thus, if a single quadrat contains the focal organism and three neighbor organisms, the plot would allocate a value of 1 for each neighbor organism (each bar on the plot), and the table would allocate a value of 1 for that quadrat (column three on the table)"),
conditionalPanel(
condition = "input.pickaplot == '1'",
p("Like the plot,", strong("the heat map"), "displays the unique number of quadrats containing the focal organism and each neighbor organism, as well as the focal organism. The darker the shade of the box, the more quadrats containing both the focal organism and the neighbor organism (or focal organism.")),
),
mainPanel("",
p(""),
plotOutput(outputId="plot_neighbor"),
br(),
br(),
gt_output(outputId="table_neighbor"),
br(),
conditionalPanel(
condition = "input.pickaplot == '1'",
plotlyOutput(outputId="plot_heatmap"))
)
)
)
)
####################################################################
# Create server
server <- function(input, output){
### TAB - Welcome
#output$home_image <- renderImage({
# filename <- normalizePath(here::here('www','quadrat.jpg'))
# print(filename)
# list(src = filename,
# width = 300)
#}, deleteFile = FALSE)
##**##**##**##**##**##
### TAB - Neighbor
### Neighbor plot
## Subset for a phylum (and location)
reef_phylum <- reactive({
reef_tidy %>%
filter(binary > "0") %>% #filter out organisms not present
mutate(focal_phylum=input$pickaphylum) %>% #pick a focal phylum (BASED ON INPUT)
mutate(to_match = ifelse(phylum==focal_phylum, filename, "FALSE")) %>% #create a column that we can subset all rows in a plot based on the presence of focal phylum in the plot at least once
filter(filename %in% to_match) %>% #if focal phylum is present, keep all observations of that plot ("filename")
distinct(filename, phylum, .keep_all=TRUE) %>% #filter for unique phylum values for each plot
filter(phylum %in% c(input$coocurring)) %>% #select only the coocurring phyla you want to look at (BASED ON INPUT)
filter(location %in% c(input$pickalocation)) #filter for location of interest
})
#Plot it up
output$plot_neighbor <- renderPlot({
ggplot(reef_phylum(), aes(x=fct_rev(forcats::fct_infreq(phylum)), fill=phylum)) +
geom_bar() +
scale_fill_manual(values = pal, guide=FALSE) + #color bars by phylum color palette, remove legend
xlab("Phylum") +
ylab(paste("Abundance in quadrats also containing",input$pickaphylum)) + #reactive y label
coord_flip() +
theme_minimal() +
theme(text = element_text(size = 15))
})
### Neighbor table
#Find number of times focal phylum makes an appearance
reef_focal <- reactive({
reef_tidy %>%
filter(binary > "0") %>% #filter out organisms not present
filter(phylum == input$pickaphylum) %>% #filter for focal phylum
filter(location %in% c(input$pickalocation)) %>% #filter for location of interest
distinct(filename) #get unique plot numbers that contain the focal phylum
})
#Find number of times neighbor genera make an appearance
reef_neighbor <- reactive({
reef_tidy %>%
filter(binary > "0") %>% #filter out organisms not present
filter(phylum %in% c(input$coocurring)) %>% #filter for neighbor phyla
filter(location %in% c(input$pickalocation)) %>% #filter for location of interest
distinct(filename) #get unique plot numbers that contain the focal phylum
})
#Find number of times focal genus co-occurs with neighbor genus
reef_together <- reactive({
reef_tidy %>%
filter(binary > "0") %>% #filter out organisms not present
mutate(to_match = ifelse(phylum %in% input$pickaphylum, filename, "FALSE")) %>% #create a column that we can subset all rows in a plot based on the presence of focal genus in the plot at least once
filter(filename %in% to_match) %>% #if focal genus is present, keep all observations of that plot ("filename")
distinct(filename, phylum, .keep_all=TRUE) %>% #filter for unique phylum values for each plot
filter(phylum %in% c(input$coocurring)) %>% #filter for neighbor phyla
filter(location %in% c(input$pickalocation)) %>% #filter for location of interest
group_by(filename) %>% #group by quadrat
summarize(sample_size = n()) %>% #get the numer of times each quadrat has an observation (of any neighbor phylum)
filter(sample_size==max(sample_size)) #only keep quadrats containing all selected neighboring phyla (AKA the "max" sample size)
})
#Put it in a nice gt() table
reef_table <- reactive({
as.data.frame(cbind(nrow(reef_focal()), nrow(reef_neighbor()), nrow(reef_together()))) %>%
mutate(percent_focal = V3/V1,
percent_neighbor = V3/V2) %>%
gt() %>%
fmt_percent(columns=vars(percent_focal, percent_neighbor), decimal=1) %>%
tab_options(table.width = pct(90)) %>% #make the table width 80% of the page width
cols_label(V1=paste("Quadrats with",input$pickaphylum),
V2="Quadrats with neighbors",
V3=paste("Quadrats with both",input$pickaphylum,"and all neighbors"),
percent_focal=paste("Percent", input$pickaphylum, "co-occurrs with neighbors"),
percent_neighbor=paste("Percent neighbors co-occur with", input$pickaphylum))
})
output$table_neighbor <- render_gt({
expr = reef_table()
})
#Generate heatmap
reef_heat <- reactive({
reef_tidy %>%
filter(location %in% c(input$pickalocation)) %>% #filter for location of interest
group_by(phylum) %>%
select(filename, binary, group_cols()) %>%
group_by(filename) %>%
distinct(filename, phylum, binary) %>%
group_by(filename, phylum) %>%
mutate(match = ifelse(length(phylum)==2, "remove", "retain")) %>%
filter(match=="retain" | binary==1) %>%
select(filename, phylum, binary) %>%
filter(binary==1,
phylum %in% c(input$pickaphylum, input$coocurring))
})
reef_heat_melt <- reactive({
crossprod(with(reef_heat(), table(filename, phylum))) %>%
as_tibble(rownames = "phylum") %>%
melt()
})
output$plot_heatmap <- renderPlotly({
ggplotly(ggplot(data=reef_heat_melt(), aes(x=phylum, y=variable, fill=value)) +
geom_tile() +
scale_fill_viridis_c(option = "B", begin = 1, end = 0.45) +
theme_minimal() +
theme(axis.text.x = element_text(angle = 35, hjust = 1, vjust = 1),
axis.title = element_blank(),
plot.margin=grid::unit(c(0,0,0,0), "mm")) +
xlab("phylum"), tooltip="all")
# ggplot(data=reef_heat_melt(), aes(x=phylum, y=variable, fill=value)) +
# geom_tile(color="white") +
# scale_fill_viridis_c(option = "B", begin = 1, end = 0.5)
})
##**##**##**##**##**##
### TAB - Community
#reactively display quadrat images for each location
output$location_image <- renderImage({
filename <- normalizePath(file.path('./www/', paste(input$locationselect, ".png", sep="")))
list(src = filename)
}, deleteFile = FALSE
)
#Community plot
#generate reactive summary data
reef_summary_community <- reactive({
reef_tidy %>%
filter(binary > "0") %>% #filter out species not present
filter(location==input$locationselect, #filter for location of interest
str_detect(orientation,pattern=input$orientationselect)) %>% #filter for orientation of interest
group_by(phylum) %>% #group by phylum
summarize(mean_count = mean(value), #get the mean count
median_count = median(value), #get the median count
sd_count = sd(value), #get the s.d. count
iqr = IQR(value), #get the interquartile range for the count
sample_size = n())
})
#generate plot
output$plot_community <- renderPlot({
ggplot(data=reef_summary_community(), aes(x=reorder(phylum, sample_size), #order bars by descending value
y=sample_size,
fill=phylum)) + #color bars by phylum identity
geom_col() +
scale_fill_manual(values=pal, limits=names(pal), guide=FALSE) + #color bars by phylum color palette, remove legend
coord_flip() +
ylab(paste("Number of plots")) +
xlab("Phylum") +
theme_minimal() +
theme(text = element_text(size = 15))
})
#Sankey diagram
#Prep data
reef_top <- reactive({
reef_summary_community() %>%
group_by(phylum) %>%
tally(sample_size) %>%
top_n(input$sankeynumber)
})
reef_sankey <- reactive({
reef_tidy %>%
filter(binary > "0") %>% #filter out species not present
filter(location==input$locationselect, #filter for location of interest
str_detect(orientation,pattern=input$orientationselect)) %>%
group_by(phylum, species) %>%
summarize(`mean abundance` = mean(value)) %>%
filter(phylum %in% c(reef_top()$phylum)) %>%
select(phylum, species, `mean abundance`)
})
reef_names <- reactive({
reef_sankey() %>%
select(phylum, species)
})
node_names <- reactive({
factor(sort(unique(as.character(unname(unlist(reef_names()))))))
})
nodes <- reactive({
data.frame(name = node_names())
})
links <- reactive({
data.frame(source = match(reef_sankey()$phylum, node_names()) - 1,
target = match(reef_sankey()$species, node_names()) - 1,
value = reef_sankey()$`mean abundance`,
group = reef_sankey()$phylum)
})
#Set color palette that can be recognized by sankeyNetwork
my_color <- 'd3.scaleOrdinal() .domain(["Annelida","Arthropoda", "Chlorophyta","Chordata","Cnidaria","Echinodermata","Ectoprocta","Fish","Heterokontophyta","Mollusca","Phoronida","Porifera","Rhodophyta"]) .range(["#D2691E", "#CDCDB4", "#3CB371", "#EE9A00","#6CA6CD", "#FF6347", "#F4A460", "#CD3700", "#6B8E23", "#708090", "#FAFAD2","#EEDD82", "#DB7093"])'
#Sankey diagram
output$sankey_plot <- renderSankeyNetwork({
sankeyNetwork(Links = links(), Nodes = nodes(),
Source = "source", Target = "target",
Value = "value", NodeID = "name",
fontSize = 12, nodeWidth = 30,
colourScale = my_color,
LinkGroup="group", NodeGroup = NULL)
})
##**##**##**##**##**##
### TAB - Abundance map
reef_summary_abundance <- reactive({
reef_tidy %>%
filter(binary > "0") %>% #filter out species not present
st_as_sf(coords=c("longitude", "latitude"), crs=4326) %>% #create sticky geometry for lat/long
filter((grouped_species==input$mapitabundance)|(phylum==input$mapitabundance)) %>% #filter by organism of interest
group_by(location) %>% #group by location
summarize(Abundance = mean(value), #get the MEAN count
sd_count = sd(value), #get the s.d. count
sample_size = n()) %>% #get the sample size
mutate(mpa = ifelse(location %in% c(mpa_sites), "mpa", "unprotected")) %>% #add column for MPA versus non-MPA sites
#filter(str_detect(mpa,pattern=input$mpaselect)) %>% #filter for orientation of interest
filter(mpa %in% c(input$mpaselect_abundance))
})
#create abundance plot
output$plot_abundance <- renderPlot({
ggplot(reef_summary_abundance(), aes(x=reorder(location, desc(location)), y=Abundance)) +
geom_col(aes(fill=Abundance)) + #fill color corresponds to value
scale_fill_viridis_c(option = "B", begin = 1, end = 0.5) + #set viridis palette to match map
xlab("Location") +
ylab(paste("Mean abundance of",input$mapitabundance)) + #reactively label y axis with map index selection
coord_flip() +
theme_minimal() +
theme(text = element_text(size = 15))
})
#create fixed coordinates of the SBC for those pesky organisms not found across the SBC
coord_sbc <- st_bbox(reef_vegan %>%
st_as_sf(coords=c("longitude", "latitude"), crs=4326))
#create abundance map
output$map_abundance <- renderLeaflet({
reef_map_abundance <- tm_basemap("Esri.WorldImagery") +
tm_shape(reef_summary_abundance(), bbox = coord_sbc) +
tm_symbols(id="location", col = "Abundance", size ="Abundance", scale=2, #point size corresponds to value
palette = "inferno", contrast = c(1,0.5)) #set viridis palette to match plot
tmap_leaflet(reef_map_abundance)
})
##**##**##**##**##**##
### TAB - Diversity map
#make vegan data reactive
reef_vegan_sf <- reactive({
reef_vegan %>%
mutate(mpa = ifelse(location %in% c(mpa_sites), "mpa", "unprotected")) %>% #add column for MPA versus non-MPA sites
#filter(str_detect(mpa,pattern=input$mpaselect)) %>% #filter for orientation of interest
filter(mpa %in% c(input$mpaselect_diversity)) %>%
st_as_sf(coords=c("longitude", "latitude"), crs=4326) #create sticky geometry for lat/long
})
#create index map
output$map_index <- renderLeaflet({
reef_map_index <- tm_basemap("Esri.WorldImagery") +
tm_shape(reef_vegan_sf(), bbox = coord_sbc) +
tm_symbols(id="location", col = input$pickanindex, size = input$pickanindex, scale=2, #point size corresponds to value
palette = "inferno", contrast = c(1,0.5)) #set viridis palette to match plot
tmap_leaflet(reef_map_index)
})
#create index plot
output$plot_index <- renderPlot({
ggplot(reef_vegan_sf(), aes(x=reorder(location, desc(location)), y=!!as.name(input$pickanindex))) +
geom_col(aes(fill=!!as.name(input$pickanindex))) + #bar fill color corresponds to map index selection
scale_fill_viridis_c(option = "B", begin = 1, end = 0.5) + #set viridis palette to match map
xlab("Location") +
ylab(paste("Species",input$pickanindex)) + #reactively label y axis with map index selection
coord_flip() +
theme_minimal() +
theme(text = element_text(size = 15))
})
##**##**##**##**##**##
### TAB - Species tree
#reactively produce image of phylum of interest
output$phylum_image <- renderImage({
filename <- normalizePath(file.path('./www/', paste(input$phylumSelectComboTree, ".png", sep="")))
list(src = filename)
}, deleteFile = FALSE
)
#create reactive URL to search for organisms (within WoRMS)
observeEvent(input$searchaphylum,{
output$url <-renderUI(a(href=paste0('https://www.google.com/search?q=', input$searchaphylum, "%20site%3Amarinespecies.org"),"Ask WoRMS!",target="_blank"))
})
#create species tree
#Set order of tree hierarchy
speciesTree <- reactive(unique(reef_tidy[reef_tidy$phylum==input$phylumSelectComboTree,
c("phylum", "grouped_genus", "grouped_species")]))
colorTree <- reactive(as.vector(pal[c(input$phylumSelectComboTree)])) #reactively generate color code for phylum of interest
output$species_tree <- renderCollapsibleTree(
collapsibleTree(
speciesTree(),
root = input$phylumSelectComboTree, #tree root is phylum of interest
attribute = "grouped_species",
hierarchy = c("grouped_genus","grouped_species"),
fill = colorTree(), #reactively fill color with phylum color palette
fontSize = 13,
zoomable = FALSE
)
)
}
####################################################################
# Let R know you want to combine ui and server into an app
shinyApp(ui=ui, server=server)