Skip to content

Instantly share code, notes, and snippets.

@lwaldron
Last active November 13, 2023 02:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lwaldron/de2875a853af9d1329e2af8513d791af to your computer and use it in GitHub Desktop.
Save lwaldron/de2875a853af9d1329e2af8513d791af to your computer and use it in GitHub Desktop.
Analyze alpha diversity by year from bugsigdb.org
library(bugsigdbr)
bsdb <- importBugSigDB(version = "devel")
# Create a stacked barplot of the proportion of Pielou, Shannon, Chao1, Simpson, Inverse Simpson, and Richness as a function of year
library(tidyverse)
bsdb_by_year <- bsdb |>
filter(Year > 2014) |>
dplyr::group_by(Year) |>
dplyr::summarize(
Pielou = sum(!is.na(Pielou)) / n(),
Shannon = sum(!is.na(Shannon)) / n(),
Chao1 = sum(!is.na(Chao1)) / n(),
Simpson = sum(!is.na(Simpson)) / n(),
InverseSimpson = sum(!is.na(`Inverse Simpson`)) / n(),
Richness = sum(!is.na(Richness)) / n()
)
bsdb_by_year_long <- bsdb_by_year |>
tidyr::pivot_longer(
cols = c(Pielou, Shannon, Chao1, Simpson, InverseSimpson, Richness),
names_to = "Metric",
values_to = "Value"
)
ggplot2::ggplot(bsdb_by_year_long, ggplot2::aes(x = Year, y = Value, fill = Metric)) +
ggplot2::geom_col(position = "stack") +
ggplot2::labs(title = "Reporting of diversity measures by year",
x = "Year",
y = "Sum of fractions reported") +
ggplot2::theme(legend.position = "bottom")
# Create a line plot from bsdb_by_year_long of all numeric columns by year
png("diversity_by_year.png", width = 4, height = 3.25, units = "in", res = 600)
ggplot2::ggplot(bsdb_by_year_long, ggplot2::aes(x = Year, y = Value*100, color = Metric)) +
ggplot2::geom_line(linewidth=1.5) +
ggplot2::labs(title = "diversity measures over time",
x = "Year",
y = "% studies reporting") +
ggplot2::theme(legend.position = "bottom")
dev.off()
system("open diversity_by_year.png")
lm(Shannon ~ Year, data = bsdb_by_year) |>
summary()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment