-
Notifications
You must be signed in to change notification settings - Fork 0
/
chpt1.bargraphs.R
156 lines (116 loc) · 5.01 KB
/
chpt1.bargraphs.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
library(tidyverse)
library(readr)
library(dplyr)
library(purrr)
library(lme4)
library(MASS)
library(glmmTMB)
library(gridExtra)
# Directory containing the transect CSV files
transect_dir <- ("/Users/user/Desktop/metadata_flow/melville_50m_transects")
# Updated function to process each CSV file
process_transect_file <- function(file_path) {
# Read the CSV file
transect_data <- read_csv(file_path, col_types = cols())
# Calculate species richness
richness <- length(unique(transect_data$label_name))
# Calculate species abundance (total number of observations)
species_abundance <- nrow(transect_data)
# Calculate frequencies for Shannon diversity if there are any observations
if(species_abundance > 0) {
species_frequencies <- table(transect_data$label_name) / species_abundance
shannon_diversity <- -sum(species_frequencies * log(species_frequencies))
# Calculate Pielou's evenness (J')
pielou_evenness <- shannon_diversity / log(richness)
} else {
# Assign default values if there are no observations
shannon_diversity <- NA
pielou_evenness <- NA
}
# Determine depth zone based on the mean depth
depth_zone <- cut(mean(transect_data$depth, na.rm = TRUE),
breaks = c(100, 300, 500, 700, 900, 1100, 1310),
labels = c("Zone 1", "Zone 2", "Zone 3", "Zone 4", "Zone 5", "Zone 6"),
include.lowest = TRUE, right = FALSE)
# Extract dive number from the file name
dive_number <- gsub(".*dive([0-9]+).*", "\\1", basename(file_path))
# Return a tibble with the calculated metrics and depth zone
tibble(dive_number = dive_number,
richness = richness,
abundance = species_abundance,
shannon_diversity = shannon_diversity,
pielou_evenness = pielou_evenness,
depth_zone = as.character(depth_zone)) # Ensure depth_zone is treated as character for consistency
}
# Directory containing CSV files
transect_dir <- "/Users/user/Desktop/metadata_flow/melville_50m_transects"
# List all CSV files in the directory
csv_files <- list.files(transect_dir, pattern = "\\.csv$", full.names = TRUE)
# Process each file and combine the results into a single dataframe
transect_info <- map_dfr(csv_files, process_transect_file)
# Print the resulting dataframe
print(transect_info)
### PLOTTING RICHNESS
# Assuming your data frame is named transect_info
average_richness <- transect_info %>%
filter(!is.na(depth_zone)) %>%
group_by(depth_zone) %>%
summarise(
mean_richness = mean(richness, na.rm = TRUE),
sd_richness = sd(richness, na.rm = TRUE),
n = n(),
se_richness = sd_richness / sqrt(n)
) %>%
select(depth_zone, mean_richness, se_richness)
p1 <- ggplot(average_richness, aes(x = depth_zone, y = mean_richness, fill = depth_zone)) +
geom_bar(stat = "identity", position = position_dodge(), width = 0.7) +
geom_errorbar(aes(ymin = mean_richness - se_richness, ymax = mean_richness + se_richness),
width = 0.25, position = position_dodge(0.7)) +
labs(title = "a)",
x = "Depth Zone",
y = "Mean Morphospecies Richness") +
scale_fill_brewer(palette = "Spectral") +
theme_minimal() +
theme(legend.title = element_blank())
## PLOTTING ABUNDANCE
average_abundance <- transect_info %>%
filter(!is.na(depth_zone)) %>% # Exclude rows where depth_zone is NA
group_by(depth_zone) %>%
summarise(
mean_abundance = mean(abundance, na.rm = TRUE),
sd_abundance = sd(abundance, na.rm = TRUE),
n = n(),
se_abundance = sd_abundance / sqrt(n)
) %>%
select(depth_zone, mean_abundance, se_abundance)
p2 <- ggplot(average_abundance, aes(x = depth_zone, y = mean_abundance, fill = depth_zone)) +
geom_bar(stat = "identity", position = position_dodge(), width = 0.7) +
geom_errorbar(aes(ymin = mean_abundance - se_abundance, ymax = mean_abundance + se_abundance),
width = 0.25, position = position_dodge(0.7)) +
labs(title = "b)",
x = "Depth Zone",
y = "Mean Morphospecies Abundance") +
scale_fill_brewer(palette = "Spectral") +
theme_minimal() +
theme(legend.title = element_blank())
# combine the plot
# Combine plots into a single panel plot with 1 column and 2 rows
panel_plot <- p1 / p2
# Print the combined plot
panel_plot
### Plotting evenness
evenness_summary <- transect_info %>%
group_by(depth_zone) %>%
summarise(
mean_pielou_evenness = mean(pielou_evenness, na.rm = TRUE),
sd_pielou_evenness = sd(pielou_evenness, na.rm = TRUE)
)
ggplot(evenness_summary, aes(x = depth_zone, y = mean_pielou_evenness, fill = depth_zone)) +
geom_bar(stat = "identity", position = position_dodge()) +
geom_errorbar(aes(ymin = mean_pielou_evenness - sd_pielou_evenness, ymax = mean_pielou_evenness + sd_pielou_evenness),
width = 0.2, position = position_dodge(0.9)) +
theme_minimal() +
labs(title = "Average Pielou's Evenness by Depth Zone",
x = "Depth Zone", y = "Average Pielou's Evenness") +
scale_fill_brewer(palette = "Spectral") +
theme(legend.title = element_blank())