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")
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  # 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 five turning events (TE1–TE5), compute the baseline CH₄/CO₂ ratios prior to each event, and visualize changes afterward. TE1, TE3, TE5 represent events where both the control and experimental piles were turned. TE2 and TE4 represent events where only the control pile was turned. The purple dashed line in each plot represents a 7 day average to establishg a pre turning baseline ratio.


Turning Event 1

Turning Event 2

Turning Event 3

Turning Event 4

Turning Event 5

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

# Days of interest
selected_days <- c(189, 209, 223, 236, 250)

# Filter only those days
Flux_subset <- Flux_Data %>%
  filter(DOY %in% selected_days)

# Run t-tests manually and store results
t_results_selected <- Flux_subset %>%
  group_by(DOY) %>%
  summarise(
    # Run the t-test
    ttest = list(t.test(fluxratio ~ Pile, data = cur_data())),
    
    # Extract useful info from each t-test object
    mean_exp = ttest[[1]]$estimate[1],
    mean_ctrl = ttest[[1]]$estimate[2],
    t_stat = ttest[[1]]$statistic,
    df = ttest[[1]]$parameter,
    p_value = ttest[[1]]$p.value,
    conf_low = ttest[[1]]$conf.int[1],
    conf_high = ttest[[1]]$conf.int[2]
  ) %>%
  select(-ttest)  # remove the full t-test object for clarity

# View results
print(t_results_selected)
## # A tibble: 4 × 8
##     DOY mean_exp mean_ctrl t_stat    df    p_value  conf_low conf_high
##   <dbl>    <dbl>     <dbl>  <dbl> <dbl>      <dbl>     <dbl>     <dbl>
## 1   209  0.0159    0.0185  -0.702  52.9 0.485      -0.0101     0.00488
## 2   223  0.0108    0.0160  -1.66   86.2 0.101      -0.0114     0.00104
## 3   236  0.00616   0.00487  1.20   48.5 0.236      -0.000866   0.00343
## 4   250  0.0197    0.00543  5.08   97.1 0.00000186  0.00870    0.0199
ggplot(t_results_selected, aes(x = DOY, y = p_value)) +
  geom_point(size = 3, color = "firebrick") +
  geom_line(color = "firebrick") +
  geom_hline(yintercept = 0.05, linetype = "dashed", color = "gray40") +
  labs(
    title = "T-tests for CH4/CO2 Ratio Differences at Selected Days",
    x = "Day of Year (DOY)",
    y = "p-value"
  ) +
  theme_minimal(base_size = 14)

Here is a t test for the overall mean ratios for both the control and experimental piles:

overall_ttest <- t.test(fluxratio ~ Pile, data = Flux_Data)
print(overall_ttest)
## 
##  Welch Two Sample t-test
## 
## data:  fluxratio by Pile
## t = -4.0429, df = 6520.3, p-value = 5.341e-05
## alternative hypothesis: true difference in means between group C and group E is not equal to 0
## 95 percent confidence interval:
##  -0.008680898 -0.003011446
## sample estimates:
## mean in group C mean in group E 
##      0.03249975      0.03834592
#here is a boxplot
ggplot(Flux_Data, aes(x = Pile, y = fluxratio, fill = Pile)) +
  geom_boxplot(alpha = 0.7, outlier.alpha = 0.1) +
  labs(
    title = "CH4/CO2 Ratio by Compost Pile",
    x = "Pile",
    y = "CH4/CO2 Flux Ratio"
  ) +
  theme_minimal(base_size = 14) +
  theme(legend.position = "none")

ggplot(Flux_Data, aes(x = Pile, y = fluxratio, fill = Pile)) +
  geom_boxplot(alpha = 0.7, outlier.alpha = 0.1) +
  scale_y_log10() +
  labs(
    title = "CH4/CO2 Ratio by Pile (log scale)",
    x = "Pile",
    y = "CH4/CO2 Flux Ratio"
  ) +
  theme_minimal(base_size = 14) +
  theme(legend.position = "none")

#violin plot
ggplot(Flux_Data, aes(x = Pile, y = fluxratio, fill = Pile)) +
  geom_violin(alpha = 0.6, trim = FALSE) +
  stat_summary(fun = mean, geom = "point", color = "black", size = 3) +
  labs(
    title = "Distribution of CH4/CO2 Ratios by Pile",
    x = "Pile",
    y = "CH4/CO2 Ratio"
  ) +
  theme_minimal(base_size = 14) +
  theme(legend.position = "none")

7. Summary

  • The CH₄/CO₂ ratio provides insight into the balance between aerobic and anaerobic decomposition.
  • Turning events reset pile conditions, often causing temporary drops in CH₄ relative to CO₂.
  • The experimental pile shows distinct ratio dynamics compared to the control, indicating treatment effects on microbial activity.