This code provides examples of visualizations shown on MERMAID Explore that focus on the habitat complexity protocol.
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 - habitat complexity scores across surveys
The following code creates a histogram showing the distribution of habitat complexity scores (0-5) across surveys (i.e. summary sample events). It uses the average habitat complexity scores per survey (sample event) but bins those averages into integers, which more closely reflects the fact that habitat complexity is scored as integers from 1-5.
Show the code
### Get all of the relevant habitat complexity data from summary sample eventshabCompSurveySummTBL <- allMermaidSampEventsTBL %>%filter(!is.na(habitatcomplexity_score_avg_avg)) habitatComplAggHist <-plot_ly(x = habCompSurveySummTBL$habitatcomplexity_score_avg_avg,type ='histogram',xbins =list(size =1),marker =list(color ="#769fca"),height =450,hovertemplate =paste('Average score: %{x}','<br>%{y} surveys','<extra></extra>')) %>%config(displayModeBar =TRUE,displaylogo =FALSE,modeBarButtonsToRemove =c('zoom','pan', 'select', 'zoomIn', 'zoomOut','autoScale', 'resetScale', 'lasso2d','hoverClosestCartesian', 'hoverCompareCartesian')) %>%layout(bargap =0.1,xaxis =list(title ="Score",linecolor ="black",linewidth =2),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 ="HABITAT COMPLEXITY", showarrow =FALSE, xref ='paper', yref ='paper', xanchor ='left', yanchor ='top',font =list(size =20)),list(x =0, y =1.08,text =paste0(length(habCompSurveySummTBL$habitatcomplexity_score_avg_avg)," 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 subtitle# Visualize the plothabitatComplAggHist
Time series - % surveys by habitat complexity score over time
Stacked barplots showing the percent of surveys (y-axis) with each habitat complexity score (colors) by year (x-axis). Different colors represent each of the six habitat complexity scores (0, 1, 2, 3, 4, 5).
Show the code
### Get the relevant habitat complexity informationhabCompNumSurveysByYearTBL <- allMermaidSampEventsTBL %>%filter(!is.na(habitatcomplexity_score_avg_avg)) %>%mutate(RoundHabitatComplexity =as.factor(round( habitatcomplexity_score_avg_avg)),year =year(sample_date)) %>%group_by(year, RoundHabitatComplexity) %>% dplyr::summarise(NumSurveysByHabCompYear =length(RoundHabitatComplexity),.groups ="keep") %>%ungroup()#Merge with the total number of surveys per year to get percentagehabCompPercSurveysByYearTBL <- habCompNumSurveysByYearTBL %>%left_join(habCompNumSurveysByYearTBL %>%group_by(year) %>%summarise(NumSurveysByYear =sum(NumSurveysByHabCompYear)) %>%ungroup(),by ="year") %>%mutate(PercSurveysByHabCompYear = NumSurveysByHabCompYear/ NumSurveysByYear *100)#### Create color map for habitat complexityhabitatComplexityColorMap <-setNames(object =c("#eff7ff", "#cbdaea", "#a8bed5", "#85a3c1", "#6288ad", "#3c6e9a"),nm =levels(habCompPercSurveysByYearTBL$RoundHabitatComplexity))habitatComplTimeSeriesBarplot <-plot_ly(data = habCompPercSurveysByYearTBL,x =~year,y =~PercSurveysByHabCompYear,type ='bar',color =~RoundHabitatComplexity,colors = habitatComplexityColorMap,height =450,hovertemplate =paste('Complexity score: %{fullData.name}','<br>Year: %{x}','<br>%{y:.1f}% of surveys','<extra></extra>')) %>%config(displayModeBar =TRUE,displaylogo =FALSE,modeBarButtonsToRemove =c('zoom','pan', 'select', 'zoomIn', 'zoomOut','autoScale', 'resetScale', 'lasso2d','hoverClosestCartesian','hoverCompareCartesian')) %>%layout(barmode ="stack",bargap =0.1,yaxis =list(title ="% of Surveys",linecolor ="black",tickvals =seq(0, 100, by =10), # Set y-axis tick valuesticktext =seq(0, 100, by =10),linewidth =2),xaxis =list(title ="Year",linecolor ="black", # Set the x-axis line color to blacklinewidth =2),annotations =list(list(x =0, y =1.15,text ="HABITAT COMPLEXITY",showarrow =FALSE,xref ='paper', yref ='paper', xanchor ='left', yanchor ='top',font =list(size =20)),list(x =0, y =1.08,text =paste0(nrow( allMermaidSampEventsTBL %>%filter(!is.na(habitatcomplexity_score_avg_avg)) ), " Surveys"),showarrow =FALSE, xref ='paper', yref ='paper', xanchor ='left', yanchor ='top',font =list(size =12)) ),legend =list(orientation ="h",xanchor ="center",x =0.5, y =-0.2),margin =list(t =50, b =75)) # Increase top margin to create more space for title and subtitle# Visualize the plothabitatComplTimeSeriesBarplot
Simple scale - Average habitat complexity for a single survey
This code creates a very simple representation of a single number for average habitat complexity score for a single survey (i.e. MERMAID sample event), along a gradient of possible scores from 0-5. The horizontal line shows the gradient and the vertical line shows the average habitat complexity score, with a box to show the actual number. Icons were added to represent the gradient from low to high habitat complexity.