This code provides examples of visualizations shown on MERMAID Explore that use data from one or more of the benthic protocols that collect information about the % cover of benthic categories (PIT, LIT, Photo Quadrat, and Bleaching surveys).
Getting summary sample event data from MERMAID
The following code downloads summary sample event data from MERMAID using the mermaidr package (documentation can be found at https://data-mermaid.github.io/mermaidr/). The summary sample events contain all surveys (i.e. sample events) that have permissions of “public summary” or “public”.
Show the code
rm(list =ls()) #remove past stored objectsoptions(scipen =999) #turn off scientific notation#### Load packages and libraries ###### If this is the first time using mermaidr, install the package through "remotes"# install.packages("remotes")# remotes::install_github("data-mermaid/mermaidr")# install.packages("tidyverse")# install.packages("plotly") # install.packages("htmlwidgets")library(mermaidr) #package to download data from datamermaid.orglibrary(tidyverse) #package that makes it easier to work with datalibrary(plotly) #for interactive plottinglibrary(htmlwidgets) #for saving plots at html files#### Get data from MERMAID for creating aggregate visualizations ####allMermaidSampEventsTBL <- mermaidr::mermaid_get_summary_sampleevents()
Histogram - Hard coral cover (%) by survey
This code creates a histogram showing the variability in % hard coral cover across surveys (sample events), with different colored bars for <10% coral cover (net-negative carbonate production), >10% but <30% coral cover (net-positive carbonate production), and >30% coral cover (maintains biodiversity, complexity, and fisheries production). It also includes code to show the total number of surveys (i.e. sample events in MERMAID) at the top of the plot. In the first step, we calculate average % hard coral cover for each survey across all four benthic protocols that measure % hard coral cover: benthic PIT, benthic LIT, benthic PQT, and bleaching surveys.
Show the code
### Combine all benthic data - get averages across 4 protocolshardCoralTBL <- allMermaidSampEventsTBL %>%rowwise() %>%mutate(all_percent_cover_avg_Hard_coral =mean(c(`benthicpit_percent_cover_benthic_category_avg_Hard coral`,`benthiclit_percent_cover_benthic_category_avg_Hard coral`,`benthicpqt_percent_cover_benthic_category_avg_Hard coral`, quadrat_benthic_percent_percent_hard_avg_avg), na.rm = T)) %>%ungroup() %>%filter(!is.na(all_percent_cover_avg_Hard_coral))# Create the histogram to get bin data to assign colorshist_data <-hist(hardCoralTBL$all_percent_cover_avg_Hard_coral,breaks =seq(0, 100, by =2),plot =FALSE) #In this function a 4 will be in the first bin# Assign colors based on bin orderbin_colors <-sapply(seq_along(hist_data$counts), function(i) {if (i <=5) {return('#d13823') } elseif (i >5&& i <=15) {return('#f3a224') } else {return('#277d1d') }})# Create the histogram using plotlyhardCoralAggHist <-plot_ly(x = hardCoralTBL$all_percent_cover_avg_Hard_coral,type ='histogram',xbins =list(start =0, size =2, end =100),marker =list(color = bin_colors), height =450) %>%config(displayModeBar =TRUE,displaylogo =FALSE,modeBarButtonsToRemove =c('zoom','pan', 'select', 'zoomIn', 'zoomOut','autoScale', 'resetScale', 'lasso2d','hoverClosestCartesian', 'hoverCompareCartesian')) %>%layout(bargap =0.1,shapes =list(list(type ="line", x0 =10, x1 =10, y0 =0, y1 =1, yref ="paper", line =list(color ="black", dash ="dot")),list(type ="line", x0 =30, x1 =30, y0 =0, y1 =1, yref ="paper", line =list(color ="black", dash ="dot")) ),xaxis =list(title ="Hard coral cover (%)",linecolor ="black",linewidth =2,tickvals =seq(0, 100, by =10), # Set x-axis tick valuesticktext =seq(0, 100, by =10)),yaxis =list(title ="Number of surveys",linecolor ="black", # Set the y-axis line color to blacklinewidth =2),annotations =list(list(x =0, y =1.15, text ="HARD CORAL COVER", showarrow =FALSE, xref ='paper', yref ='paper', xanchor ='left', yanchor ='top',font =list(size =20)),list(x =0, y =1.08,text =paste0(length(hardCoralTBL$all_percent_cover_avg_Hard_coral)," Surveys"),showarrow =FALSE, xref ='paper', yref ='paper', xanchor ='left', yanchor ='top',font =list(size =12)) ),margin =list(t =50, b =75)) %>%# Increase top margin to create more space for title and subtitlestyle(hovertemplate ="Bin: %{x}%<br>%{y} surveys<extra></extra>") # Visualize the plothardCoralAggHist
Time series - Benthic cover (%) by year and category
Stacked barplot showing the percent cover (y-axis) by year (x-axis) for each of 11 high-level benthic categories: Hard coral, Bare substrate, Crustose coralline algae, Rubble, Cyanobacteria, Seagrass, Sand, Macroalgae, Turf algae, Soft coral, Other invertebrates. In the first step, average % cover is calculated for each of the 11 categories across the three benthic protocols that collect data for those categories (benthic PIT, benthic LIT, benthic PQT).
Stacked Single Barplot - % cover by category (single survey)
Code to create a single stacked barplot showing the percent cover (y-axis), with different colors representing 11 broad level benthic categories: Hard coral, Bare substrate, Crustose coralline algae, Rubble, Cyanobacteria, Seagrass, Sand, Macroalgae, Turf algae, Soft coral, Other invertebrates. This code shows an example with a single sample event (i.e. survey) but could be adapted to apply to multiple surveys and sites. As in MERMAID Explore, this visualization also just focuses on one method - in this case benthic PIT - but could be used for others.