install.packages("ggplot2")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.5'
## (as 'lib' is unspecified)
library(ggplot2)
install.packages("tidyverse")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.5'
## (as 'lib' is unspecified)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.2.0     ✔ readr     2.2.0
## ✔ forcats   1.0.1     ✔ stringr   1.6.0
## ✔ lubridate 1.9.5     ✔ tibble    3.3.1
## ✔ purrr     1.2.1     ✔ tidyr     1.3.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
Full_EcoEv_Data_Sheet1 <- read_csv("Full_EcoEv_Data - Sheet1.csv")
## Rows: 39 Columns: 4
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): Leaf Type, Time Point, Taxon
## dbl (1): Number
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
full = Full_EcoEv_Data_Sheet1
library(dplyr)



summary_data <- full %>%
  group_by(Taxon, `Leaf Type`, `Time Point`) %>%
  summarise(
    mean = mean(Number),
    se = sd(Number) / sqrt(n()),
    .groups = "drop"
  )

ggplot(summary_data, aes(x = Taxon, y = mean, fill = `Leaf Type`)) +
  geom_col(position = position_dodge(width = 0.9)) +
  geom_errorbar(
    aes(ymin = mean - se, ymax = mean + se),
    position = position_dodge(width = 0.9),
    width = 0.2
  ) +
  theme_bw() +
  labs(
    x = "Taxon",
    y = "Mean Count",
    title = "Mean Counts with Standard Error"
  ) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  facet_wrap(~ `Time Point`)

ggplot(full, aes(x = `Leaf Type`, y = Number, fill = `Leaf Type`)) +
  geom_boxplot() +
  theme_bw() +
  labs(
    x = "Leaf Type",
    y = "Macroinvertebrate Count",
    title = "Distribution of Macroinvertebrates by Leaf Type"
  )+ theme(legend.position = "none")

shannon_time <- full %>%
  group_by(`Leaf Type`, `Time Point`) %>%
  mutate(
    total = sum(Number),
    p = Number / total
  ) %>%
  summarise(
    H = -sum(p * log(p)),
    .groups = "drop"
  )

shannon_time
## # A tibble: 6 × 3
##   `Leaf Type` `Time Point`     H
##   <chr>       <chr>        <dbl>
## 1 Magnolia    2 weeks      0.721
## 2 Magnolia    4 weeks      0.890
## 3 Magnolia    6 Weeks      1.34 
## 4 Oak         2 weeks      1.21 
## 5 Oak         4 weeks      1.23 
## 6 Oak         6 Weeks      1.52
shannon_leaf <- full %>%
  group_by(`Leaf Type`) %>%
  mutate(
    total = sum(Number),
    p = Number / total
  ) %>%
  summarise(
    H = -sum(p * log(p)),
    .groups = "drop"
  )

shannon_leaf
## # A tibble: 2 × 2
##   `Leaf Type`     H
##   <chr>       <dbl>
## 1 Magnolia     2.07
## 2 Oak          2.40
library(dplyr)

shannon_summary <- shannon_time %>%
  group_by(`Time Point`, `Leaf Type`) %>%
  summarise(
    mean_H = mean(H),
    se_H = sd(H) / sqrt(n()),
    .groups = "drop"
  )


ggplot(shannon_time, aes(x = `Time Point`, y = H, color = `Leaf Type`, group = `Leaf Type`)) +
  geom_line(size = 1) +
  geom_point(size = 3) +
  theme_classic(base_size = 14) +
  labs(
    x = "Time Point",
    y = "Shannon Diversity (H)",
    title = "Changes in Diversity Over Time by Leaf Type",
    color = "Leaf Type"
  ) +
  theme(
    axis.text = element_text(color = "black"),
    axis.title = element_text(face = "bold"),
    plot.title = element_text(face = "bold", hjust = 0.5)
  )
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once per session.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

ggplot(shannon_leaf, aes(x = `Leaf Type`, y = H, fill = `Leaf Type`)) +
  geom_col(width = 0.6, color = "black") +
  theme_classic(base_size = 14) +
  labs(
    x = "Leaf Type",
    y = "Shannon Diversity (H)",
    title = "Shannon Diversity by Leaf Type"
  ) +
  theme(
    legend.position = "none",
    axis.text = element_text(color = "black"),
    axis.title = element_text(face = "bold"),
    plot.title = element_text(face = "bold", hjust = 0.5)
  )