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.
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
)
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
The following plot shows raw daily points and a smoothed generalized additive model (gam) curve for each pile type.
The following plot shows raw daily points and a smoothed generalized
additive model (gam) curve separated for each pile type. Vertical lines
in green are turning events where both piles were turned, vertical lines
in blue are turning events where only the control pile was turned.
## List of 136
## $ line :List of 6
## ..$ colour : chr "black"
## ..$ linewidth : num 0.5
## ..$ linetype : num 1
## ..$ lineend : chr "butt"
## ..$ arrow : logi FALSE
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_line" "element"
## $ rect :List of 5
## ..$ fill : chr "white"
## ..$ colour : chr "black"
## ..$ linewidth : num 0.5
## ..$ linetype : num 1
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_rect" "element"
## $ text :List of 11
## ..$ family : chr ""
## ..$ face : chr "plain"
## ..$ colour : chr "black"
## ..$ size : num 11
## ..$ hjust : num 0.5
## ..$ vjust : num 0.5
## ..$ angle : num 0
## ..$ lineheight : num 0.9
## ..$ margin : 'margin' num [1:4] 0points 0points 0points 0points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : logi FALSE
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ title : NULL
## $ aspect.ratio : NULL
## $ axis.title : NULL
## $ axis.title.x :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : NULL
## ..$ vjust : num 1
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 2.75points 0points 0points 0points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ axis.title.x.top :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : NULL
## ..$ vjust : num 0
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 0points 0points 2.75points 0points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ axis.title.x.bottom : NULL
## $ axis.title.y :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : NULL
## ..$ vjust : num 1
## ..$ angle : num 90
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 0points 2.75points 0points 0points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ axis.title.y.left : NULL
## $ axis.title.y.right :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : NULL
## ..$ vjust : num 1
## ..$ angle : num -90
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 0points 0points 0points 2.75points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ axis.text :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : chr "grey30"
## ..$ size : 'rel' num 0.8
## ..$ hjust : NULL
## ..$ vjust : NULL
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : NULL
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ axis.text.x :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : NULL
## ..$ vjust : num 1
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 2.2points 0points 0points 0points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ axis.text.x.top :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : NULL
## ..$ vjust : num 0
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 0points 0points 2.2points 0points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ axis.text.x.bottom : NULL
## $ axis.text.y :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : num 1
## ..$ vjust : NULL
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 0points 2.2points 0points 0points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ axis.text.y.left : NULL
## $ axis.text.y.right :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : num 0
## ..$ vjust : NULL
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 0points 0points 0points 2.2points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ axis.text.theta : NULL
## $ axis.text.r :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : num 0.5
## ..$ vjust : NULL
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 0points 2.2points 0points 2.2points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ axis.ticks : list()
## ..- attr(*, "class")= chr [1:2] "element_blank" "element"
## $ axis.ticks.x : NULL
## $ axis.ticks.x.top : NULL
## $ axis.ticks.x.bottom : NULL
## $ axis.ticks.y : NULL
## $ axis.ticks.y.left : NULL
## $ axis.ticks.y.right : NULL
## $ axis.ticks.theta : NULL
## $ axis.ticks.r : NULL
## $ axis.minor.ticks.x.top : NULL
## $ axis.minor.ticks.x.bottom : NULL
## $ axis.minor.ticks.y.left : NULL
## $ axis.minor.ticks.y.right : NULL
## $ axis.minor.ticks.theta : NULL
## $ axis.minor.ticks.r : NULL
## $ axis.ticks.length : 'simpleUnit' num 2.75points
## ..- attr(*, "unit")= int 8
## $ axis.ticks.length.x : NULL
## $ axis.ticks.length.x.top : NULL
## $ axis.ticks.length.x.bottom : NULL
## $ axis.ticks.length.y : NULL
## $ axis.ticks.length.y.left : NULL
## $ axis.ticks.length.y.right : NULL
## $ axis.ticks.length.theta : NULL
## $ axis.ticks.length.r : NULL
## $ axis.minor.ticks.length : 'rel' num 0.75
## $ axis.minor.ticks.length.x : NULL
## $ axis.minor.ticks.length.x.top : NULL
## $ axis.minor.ticks.length.x.bottom: NULL
## $ axis.minor.ticks.length.y : NULL
## $ axis.minor.ticks.length.y.left : NULL
## $ axis.minor.ticks.length.y.right : NULL
## $ axis.minor.ticks.length.theta : NULL
## $ axis.minor.ticks.length.r : NULL
## $ axis.line : list()
## ..- attr(*, "class")= chr [1:2] "element_blank" "element"
## $ axis.line.x : NULL
## $ axis.line.x.top : NULL
## $ axis.line.x.bottom : NULL
## $ axis.line.y : NULL
## $ axis.line.y.left : NULL
## $ axis.line.y.right : NULL
## $ axis.line.theta : NULL
## $ axis.line.r : NULL
## $ legend.background : list()
## ..- attr(*, "class")= chr [1:2] "element_blank" "element"
## $ legend.margin : 'margin' num [1:4] 5.5points 5.5points 5.5points 5.5points
## ..- attr(*, "unit")= int 8
## $ legend.spacing : 'simpleUnit' num 11points
## ..- attr(*, "unit")= int 8
## $ legend.spacing.x : NULL
## $ legend.spacing.y : NULL
## $ legend.key : list()
## ..- attr(*, "class")= chr [1:2] "element_blank" "element"
## $ legend.key.size : 'simpleUnit' num 1.2lines
## ..- attr(*, "unit")= int 3
## $ legend.key.height : NULL
## $ legend.key.width : NULL
## $ legend.key.spacing : 'simpleUnit' num 5.5points
## ..- attr(*, "unit")= int 8
## $ legend.key.spacing.x : NULL
## $ legend.key.spacing.y : NULL
## $ legend.frame : NULL
## $ legend.ticks : NULL
## $ legend.ticks.length : 'rel' num 0.2
## $ legend.axis.line : NULL
## $ legend.text :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : 'rel' num 0.8
## ..$ hjust : NULL
## ..$ vjust : NULL
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : NULL
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ legend.text.position : NULL
## $ legend.title :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : num 0
## ..$ vjust : NULL
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : NULL
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ legend.title.position : NULL
## $ legend.position : chr "right"
## $ legend.position.inside : NULL
## $ legend.direction : NULL
## $ legend.byrow : NULL
## $ legend.justification : chr "center"
## $ legend.justification.top : NULL
## $ legend.justification.bottom : NULL
## $ legend.justification.left : NULL
## $ legend.justification.right : NULL
## $ legend.justification.inside : NULL
## $ legend.location : NULL
## $ legend.box : NULL
## $ legend.box.just : NULL
## $ legend.box.margin : 'margin' num [1:4] 0cm 0cm 0cm 0cm
## ..- attr(*, "unit")= int 1
## $ legend.box.background : list()
## ..- attr(*, "class")= chr [1:2] "element_blank" "element"
## $ legend.box.spacing : 'simpleUnit' num 11points
## ..- attr(*, "unit")= int 8
## [list output truncated]
## - attr(*, "class")= chr [1:2] "theme" "gg"
## - attr(*, "complete")= logi TRUE
## - attr(*, "validate")= logi TRUE
The following plot shows gas flux ratios averaged daily with standard error bars in black.
To smooth variability, we averaged by week and plot the weekly mean ratios.
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.
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")