setwd("~/Desktop/inPrep/pew/github/erp/Rmd")

Consumption of forage fish in Ecopath

This is an R Markdown document for the visualization of outputs from the Ecopath component of Ecopath with Ecosim (EwE) models of Northeast Atlantic regional seas. Models used in this work include those covering the extent of:

  1. The Irish Sea (ICES VIIa),
  2. The Celtic Sea (ICES VIIe, VIIf, VIIh, VIIj.2),
  3. The West Coast of Scotland (VIa),
  4. The Greater North Sea (IVa, IVb, IVc),
  5. The Northern North Sea (IVa),
  6. The Norwegian Sea and Barents Sea (Ia, Ib, IIa, IIb)

Ecopath components for all of the above models were available for this study. From these models, covering a single year snapshot, we have extracted the estimated consumption (tonnes per km-2) for key forage fish species. This analysis provides an overview of the ecosystem role of species in the available ecosystem models and enables regional comparison of 1) the quantity of forage fish species consumed and 2) the quantity of forage fish consumed by predator species.

# Code to create an ordered stacked bar char for the consumption of forage fish in each modelled region

# Create a new data frame with the sum of Consumption by Region
Forage_sum <- aggregate(Consumption ~ Region, data = Forage, FUN = sum)

# Order the Region variable based on the sum of Consumption
Forage_sum_ordered <- Forage_sum[order(Forage_sum$Consumption, decreasing = TRUE), ]

# Create the ordered stacked bar plot for forage fish consumed in each region
# Define the color palette from viridis for all prey categories
prey_colors <- viridis_pal(option = "D")(10)

# Set the "Unspecified" category to light grey
prey_colors[10] <- "lightgrey"

FF_consumption <- ggplot(Forage, aes(x = reorder(Region, -Consumption, FUN = sum), y = Consumption, fill = Prey)) +
  geom_bar(stat = "identity") +
  labs(x = "Model region", y = expression("Forage fish consumed t." ~ km^{-2}), fill = "Forage fish") +
  scale_fill_manual(name = "Forage fish", values = prey_colors) +
  theme_minimal() +
  theme(panel.background = element_rect(fill = "white", color = "black"),
        axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1))
FF_consumption
Figure 1. Estimates of forage fish consumption (tonnes per km-2) in the Ecopath models of Northeast Atlantic regional Seas. Values represent the consumption in the base year of the model.

Figure 1. Estimates of forage fish consumption (tonnes per km-2) in the Ecopath models of Northeast Atlantic regional Seas. Values represent the consumption in the base year of the model.

ggsave("../outputs/figs/FF_consumption.png", FF_consumption, dpi = 500)
## Saving 7 x 5 in image
# Code to create an ordered stacked bar char for the consumption of forage fish by predator

# Create a new data frame with the sum of Consumption by Region
Forage_sum <- aggregate(Consumption ~ Region, data = Forage, FUN = sum)

# Order the Region variable based on the sum of Consumption
Forage_sum_ordered <- Forage_sum[order(Forage_sum$Consumption, decreasing = TRUE), ]

# Create the ordered stacked bar plot for forage fish consumption by predator
FF_preds<-ggplot(Forage, aes(x = reorder(Region, -Consumption, FUN = sum), y = Consumption, fill = Predator)) +
  geom_bar(stat = "identity") +
  labs(x = "Model region", y = expression("Forage fish consumed t." ~ km^{-2}), fill = "Forage fish") +
  scale_fill_viridis_d(name = "Predator") +
  theme_minimal() +
  theme(panel.background = element_rect(fill = "white", color = "black"),
        axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1))

ggsave("../outputs/figs/FF_preds.png", FF_preds, dpi = 500)
## Saving 7 x 5 in image
FF_preds
Figure 2. Estimates of forage fish consumed by predators (tonnes per km-2) in the Ecopath models of Northeast Atlantic regional Seas. Values represent the consumption in the base year of the model.

Figure 2. Estimates of forage fish consumed by predators (tonnes per km-2) in the Ecopath models of Northeast Atlantic regional Seas. Values represent the consumption in the base year of the model.

library(cowplot)

# Your plot objects (FF_consumption and FF_preds)
# ...

# Add titles to the plots
plot_with_title1 <- plot_grid(FF_consumption + 
                                 ggtitle("a)"),
                              align = "v",
                              ncol = 1)

plot_with_title2 <- plot_grid(FF_preds + 
                               ggtitle("b)"),
                             align = "v",
                             ncol = 1)

# Arrange the plots with titles
grid_arranged <- plot_grid(plot_with_title1, plot_with_title2,
                           ncol = 2)

# Print or display the arranged plots
print(grid_arranged)

ggsave("../outputs/figs/FF_grid_plot.png", grid_arranged, dpi = 500, width=10)
## Saving 10 x 5 in image

Dissimilarity between ecosystem function

Dissimilarity analyses were generated to compare the structure of modelled regions in terms of the similarity of 1) the forage fish community consumed and 2) the predators responsible for forage fish consumption.

# Function for the calaculation of a dissimilarity matrix and cluster visualisation 

plotDissimilarity <- function(data, preyColumnName) {
  # Prepare the data
  consumption_matrix <- reshape2::dcast(data, Region ~ get(preyColumnName), value.var = "Consumption")
  names <- consumption_matrix[, 1]
  consumption_matrix <- consumption_matrix[, -1]  # Remove the 'Region' column

  # Convert matrix values to numeric
  consumption_matrix <- apply(consumption_matrix, 2, function(x) as.numeric(as.character(x)))

  # Remove rows with missing values
  consumption_matrix <- consumption_matrix[complete.cases(consumption_matrix), ]

  if (length(consumption_matrix) == 0) {
    stop("No valid data found for the specified prey column.")
  }

  # Set row names of consumption_matrix
  rownames(consumption_matrix) <- names

  # Compute dissimilarity matrix
  dissimilarity_matrix <- dist(consumption_matrix)

  # Set up colour blind friendly palette
  color_palette <- viridis(n = 256)

  # Visualize dissimilarity matrix as a heatmap with cluster links
  pheatmap(as.matrix(dissimilarity_matrix), cluster_rows = TRUE, cluster_cols = FALSE,
           col = color_palette, main = paste("Cluster analysis of modelled regions:", preyColumnName),
           xlab = "Predator", ylab = "Region",
           key = TRUE, keysize = 1.0, key.title = "Dissimilarity")
}
FF_dis<-plotDissimilarity(Forage, "Prey")
## Aggregation function missing: defaulting to length
ggsave("../outputs/figs/FF_dis.png", FF_dis, dpi = 500)
## Saving 7 x 5 in image
FF_dis
Figure 3. Cluster and dissimilarity analysis of the community composition of forage fish consumed in the modelled regions.

Figure 3. Cluster and dissimilarity analysis of the community composition of forage fish consumed in the modelled regions.

Pred_dis<-plotDissimilarity(Forage, "Predator")
## Aggregation function missing: defaulting to length
ggsave("../outputs/figs/Pred_dis.png", Pred_dis, dpi = 500)
## Saving 7 x 5 in image
Pred_dis
Figure 4. Cluster and dissimilarity analysis of the predator community responsible for the consumption of forage fish in the modelled regions.

Figure 4. Cluster and dissimilarity analysis of the predator community responsible for the consumption of forage fish in the modelled regions.