5. Taxonomic composition and barplots
Summarise taxonomic composition across salinity gradients and seasons using stacked barplots.
Define the barplot function
The function below aggregates monthly averages within salinity intervals (roughly corresponding to the basins Bothnian Bay, Bothnian Sea, Baltic Proper, Kattegat and Skagerrak) and plots stacked barplots for the top taxa.
plot_barplots <- function(rank, size_taxa = -1, main = "", top_x = 10) {
mycols <- colorRampPalette(c(
"#a6cee3", "#1f78b4", "#b2df8a", "#33a02c",
"#fb9a99", "#e31a1c", "#fdbf6f", "#ff7f00",
"#cab2d6", "#6a3d9a", "#ffff99", "#b15928"
))
par(mfrow = c(5, 1), mar = c(2, 3, 2, 14), xpd = TRUE)
salinity_intervals <- rbind(c(0, 3), c(3, 6), c(6, 12), c(12, 20), c(20, 35))
ok <- sort(rowMeans(cladecounts_df$norm[[rank]]), index.return = TRUE, decreasing = TRUE)$ix[1:top_x]
if (size_taxa == -1) { size_taxa <- min(1.5, 8 / length(ok)) }
for (i in 1:5) {
ix <- intersect(which(salinity >= salinity_intervals[i,1]), which(salinity < salinity_intervals[i,2]))
monthly_averages_matr <- matrix(ncol = 12, nrow = nrow(cladecounts_df$norm[[rank]]))
for (j in 1:12) {
ix2 <- intersect(ix, which(month == j))
if (length(ix2) > 1) { monthly_averages_matr[, j] <- rowMeans(cladecounts_df$norm[[rank]][, ix2]) }
if (length(ix2) == 1) { monthly_averages_matr[, j] <- cladecounts_df$norm[[rank]][, ix2] }
if (length(ix2) == 0) { monthly_averages_matr[, j] <- 0 }
}
barplot(monthly_averages_matr[ok, ], col = mycols(length(ok)),
main = paste("Salinity", salinity_intervals[i,1], "-", salinity_intervals[i,2]))
legend("bottomleft", bty = "n", pch = 19,
col = mycols(length(ok))[length(ok):1],
cex = size_taxa, inset = c(1, 0),
legend = rownames(cladecounts_df$norm[[rank]])[rev(ok)])
}
}Note on taxonomic ranks
Use numeric rank codes where: 1 = kingdom (or domain), 2 = phylum, 3 = class, 4 = order, 5 = family, 6 = genus, 7 = species.
Some datasets may also contain OTU-level identifiers (e.g. BOLD-BINs, Unite SHs).
Example: plot top classes
Plot the 8 most abundant classes:
plot_barplots(rank = 3, top_x = 8)This will create five stacked barplot rows (one per salinity interval), each showing monthly averages for the selected taxa. You can also adjust the size of the taxa legend with the size_taxa parameter.
← Previous · Overview · Next →