library(colorspace)
#### CUSTOMIZE: Define your target fish families ####
# List the family names (lowercase) that are of particular interest in your region.
# For example, food fish families.
# Any families not listed here will be grouped into "Other families".
target_families <- c(
"acanthuridae",
"balistidae",
"caesionidae",
"carangidae",
"ephippidae",
"haemulidae",
"holocentridae",
"kyphosidae",
"labridae",
"lethrinidae",
"lutjanidae",
"mullidae",
"nemipteridae",
"pomacanthidae",
"scaridae",
"scombridae",
"siganidae",
"sphyraenidae"
)
#### CUSTOMIZE: Pretty labels for each family ####
family_labels <- c(
acanthuridae = "Acanthuridae (surgeonfish)",
balistidae = "Balistidae (triggerfish)",
caesionidae = "Caesionidae (fusiliers)",
carangidae = "Carangidae (jacks)",
ephippidae = "Ephippidae (spade/batfish)",
haemulidae = "Haemulidae (sweetlips)",
holocentridae = "Holocentridae (soldier/squirrelfish)",
kyphosidae = "Kyphosidae (sea chubs)",
labridae = "Labridae (wrasse)",
lethrinidae = "Lethrinidae (emperors)",
lutjanidae = "Lutjanidae (snappers)",
mullidae = "Mullidae (goatfish)",
nemipteridae = "Nemipteridae (breams)",
pomacanthidae = "Pomacanthidae (angelfish)",
scaridae = "Scaridae (parrotfish)",
scombridae = "Scombridae (mackerels/tunas)",
siganidae = "Siganidae (rabbitfish)",
sphyraenidae = "Sphyraenidae (barracudas)",
other = "Other families"
)
#### Reshape data by fish family ####
fish_family_tbl <- fish_tbl %>%
select(
community, site_label, latitude, longitude,
biomass_kgha_avg,
starts_with("biomass_kgha_fish_family_avg_")
) %>%
pivot_longer(
starts_with("biomass_kgha_fish_family_avg_"),
names_to = "family",
values_to = "biomass",
names_prefix = "biomass_kgha_fish_family_avg_"
) %>%
mutate(
biomass = ifelse(is.na(biomass), 0, biomass),
family = if_else(family %in% target_families, family, "other")
) %>%
group_by(community, site_label, latitude, longitude,
biomass_kgha_avg, family) %>%
summarise(biomass = sum(biomass), .groups = "drop") %>%
mutate(
family = recode(family, !!!family_labels),
community = factor(community, levels = community_levels),
site_label = factor(site_label, levels = rev(site_levels))
) %>%
mutate(
family = factor(family),
family = factor(
family,
levels = c(
setdiff(levels(family), "Other families"),
"Other families"
)
)
)
max_biomass_fam <- max(fish_family_tbl$biomass_kgha_avg, na.rm = TRUE)
# Dynamic axis (same logic as trophic group plot)
axis_break_fam <- if (max_biomass_fam > 10000) {
2000
} else if (max_biomass_fam > 5000) {
1000
} else {
500
}
label_padding_fam <- max_biomass_fam * 0.08
axis_max_fam <- ceiling((max_biomass_fam + label_padding_fam) / axis_break_fam) * axis_break_fam
#### Colors ####
family_levels_vec <- levels(fish_family_tbl$family)
n_fam <- length(family_levels_vec)
n_main <- n_fam - 1
main_cols <- qualitative_hcl(n_main, palette = "Dark 3")
family_colors <- setNames(c(main_cols, "grey40"), family_levels_vec)
#### Create the plot ####
family_biomass_plot <- ggplot(
data = fish_family_tbl,
aes(x = site_label, y = biomass, fill = family)
) +
geom_bar(position = "stack", stat = "identity", alpha = 0.9,
color = "black", linewidth = 0.25) +
geom_hline(yintercept = yellow_threshold, colour = "#f3a224",
linewidth = 1) +
geom_hline(yintercept = green_threshold, colour = "#277d1d",
linewidth = 1) +
labs(x = "", y = "Fish Biomass (kg/ha)", fill = "Fish family",
title = paste0(project_name, " — Fish Biomass by Fish Family")) +
coord_flip() +
scale_fill_manual(values = family_colors) +
geom_text(
aes(x = site_label, y = biomass_kgha_avg + (max_biomass_fam * 0.01),
label = round(biomass_kgha_avg, 0)),
size = 3, hjust = 0, vjust = 0.5
) +
scale_y_continuous(
expand = c(0, 0),
limits = c(0, axis_max_fam),
breaks = seq(0, axis_max_fam, by = axis_break_fam),
labels = seq(0, axis_max_fam, by = axis_break_fam)
) +
theme_classic() +
theme(
axis.text = element_text(size = 11, colour = "black"),
axis.line = element_line(colour = "black"),
axis.ticks = element_line(colour = "black"),
axis.title = element_text(size = 12, colour = "black"),
plot.subtitle = element_text(colour = "black", size = 11, hjust = 0.5),
legend.background = element_rect(fill = "white", color = NA),
legend.position = "right",
plot.title = element_text(colour = "black", size = 14,
hjust = 0.5, face = "bold"),
legend.box.background = element_blank(),
legend.key = element_rect(color = "black", linewidth = 0.25),
legend.title = element_text(colour = "black", face = "bold"),
plot.margin = unit(c(0.2, 1, 0, 0.2), "cm"),
axis.ticks.y = element_blank(),
axis.line.x = element_line(color = "black"),
axis.line.y = element_blank(),
axis.ticks.x = element_line(color = "black"),
axis.text.y = element_text(hjust = 0.5, size = 8),
panel.border = element_blank(),
strip.background = element_rect(fill = "grey90", colour = NA),
strip.text.y = element_text(face = "bold")
)
# Only add faceting if there are multiple meaningful communities
if (length(community_levels) > 1 &&
!identical(community_levels, "All sites")) {
family_biomass_plot <- family_biomass_plot +
facet_grid(
community ~ .,
scales = "free_y",
space = "free_y"
)
}
# Save the plot
ggsave("../plots/fishFamilyBiomassPlot.svg",
plot = family_biomass_plot,
width = 9, height = 10)
family_biomass_plot