1. Introduction

The purpose of this code is to analyze the ratio of methane (CH₄) to carbon dioxide (CO₂) fluxes over time in experimental and control compost piles.
We visualize how this ratio changes throughout the composting period and after major turning events.


2. Data Import and Preparation

We load filtered datasets for CO₂ and CH₄ fluxes, then merge them by DOY (day of year). This ensures we only use data points that are deemed “clean” for both gases.

setwd("~/Desktop/Hudson Carbon/CTD Paper/data")
Dioxide_Data <- read_csv("CO2_R_2_Filtered.csv")
Methane_Data <- read_csv("CH4_R_2_Filtered.csv")

Flux_Data <- inner_join(Dioxide_Data, Methane_Data, by = "DOY") %>%
  select(
    DOY,
    CO2 = FCO2_DRY.x,
    CH4 = FCH4_DRY.y,
    Pile = Pile.x
  ) %>%
  mutate(
    DOY = floor(DOY),
    CH4_converted = CH4 / 1000,      # Convert nmol → µmol
    fluxratio = CH4_converted / CO2,
    log_ratio = log1p(fluxratio)  # Compute CH4/CO2 ratio
  )

3. Daily Summary Statistics

We calculate mean, standard deviation (SD), and standard error (SE) of the CH₄/CO₂ ratio per pile per day.

ratio_summary <- Flux_Data %>%
  group_by(DOY, Pile) %>%
  summarise(
    mean_ratio = mean(fluxratio, na.rm = TRUE),
    sd_ratio = sd(fluxratio, na.rm = TRUE),
    n = n(),
    se_ratio = sd_ratio / sqrt(n),
    .groups = "drop"
  )

head(ratio_summary)
## # A tibble: 6 × 6
##     DOY Pile  mean_ratio sd_ratio     n se_ratio
##   <dbl> <chr>      <dbl>    <dbl> <int>    <dbl>
## 1   152 E       0.000476 NA           1 NA      
## 2   154 E       0.00494   0.00641    14  0.00171
## 3   155 C       0.0214    0.00435     3  0.00251
## 4   155 E       0.0110    0.0148     13  0.00410
## 5   156 C       0.0948    0.0794      3  0.0459 
## 6   156 E       0.0877    0.0518     14  0.0139

5. Weekly Averaging

To smooth variability, we averaged by week and plot the weekly mean ratios.


6. Turning Event Analysis

The compost piles were turned at specific times to aerate the material.
We define eleven turning events (TE1–TE11), compute the baseline CH₄/CO₂ ratios prior to each event, and visualize changes afterward.

Turning events where both control and experimental piles were turned:
TE1, TE3, TE5, TE7, TE9, TE11

Turning events where only the control pile was turned:
TE2, TE4, TE6, TE8, TE10

The purple dashed line in each plot represents a 7-day pre-turning average used to establish a baseline CH₄/CO₂ ratio.

Each plot spans the 14 days after turning.


Turning Event 1

Turning Event 2

Turning Event 3

Turning Event 4

Turning Event 5

### Turning Event 6

### Turning Event 7

### Turning Event 8

### Turning Event 9

### Turning Event 10

### Turning Event 11

Next we will run t tests to look at the ratios of CH4/CO2 for both piles one day after turning.

7. Stats

selected_days <- c(188, 208, 222, 235, 249)

t_results_selected <- Flux_Data %>%
  filter(DOY %in% selected_days) %>%
  group_by(DOY) %>%
  summarise(
    p_value = t.test(fluxratio ~ Pile)$p.value,
    .groups = "drop"
  )

ggplot(t_results_selected, aes(x = DOY, y = p_value)) +
  geom_point(size = 3) +
  geom_line() +
  geom_hline(yintercept = 0.05, linetype = "dashed") +
  labs(
    title = "P-values Across Selected Days",
    x = "DOY",
    y = "p-value"
  ) +
  theme_minimal()

8. Annotated Time Series Plot

turning_days <- c(188, 208, 222, 235, 249)

ggplot(Flux_Data, aes(x = DOY, y = fluxratio, color = Pile)) +
  geom_point(alpha = 0.1, size = 1) +
  geom_smooth(method = "loess", span = 0.3, se = TRUE, size = 1.2) +
  geom_vline(xintercept = turning_days, linetype = "dashed", color = "black", alpha = 0.6) +
  scale_y_continuous(trans = "log1p") +
  labs(
    title = "Temporal Dynamics of CH4/CO2 Flux Ratio",
    subtitle = "Dashed lines indicate turning events",
    x = "Day of Year (DOY)",
    y = "CH4 / CO2 Ratio (log1p scale)",
    color = "Pile"
  ) +
  theme_minimal()