plant weight

df %>%
  ggplot(aes(x = as.numeric(as.character(N)), y = weight, color = as.factor(N))) +
  geom_boxplot() +
  scale_x_continuous(breaks = c(50, 100, 150, 200)) +
  labs(x = "N Concentration", y = "Plant Weight", color = "N") +
  theme_bw() +
  facet_wrap(vars(date))

anova

# Step 1: Perform ANOVA and Tukey-Kramer Test for Each Date
detailed_results <- df %>%
  group_by(date) %>%
  summarise(
    ANOVA = list(aov(weight ~ as.factor(N), data = cur_data())),
    Tukey = list(TukeyHSD(aov(weight ~ as.factor(N), data = cur_data())))
  ) %>%
  mutate(
    ANOVA_Summary = map(ANOVA, ~summary(.x)),
    Tukey_Summary = map(Tukey, ~broom::tidy(.x))
  )

# Step 2: Prepare ANOVA Results Table
anova_table <- detailed_results %>%
  select(date, ANOVA) %>%
  mutate(
    ANOVA_Statistics = map(ANOVA, ~broom::tidy(.x))
  ) %>%
  unnest(ANOVA_Statistics) %>%
  rename(
    Date = date,
    Term = term,
    Degrees_of_Freedom = df,
    Sum_Squares = sumsq,
    Mean_Square = meansq,
    F_Value = statistic,
    P_Value = p.value
  )

# Step 3: Prepare Tukey Post-Hoc Results Table
tukey_table <- detailed_results %>%
  select(date, Tukey_Summary) %>%
  unnest(Tukey_Summary) %>%
  rename(
    Date = date,
    Comparison = term,
    Mean_Difference = estimate,
    Lower_CI = conf.low,
    Upper_CI = conf.high,
    Adjusted_p_value = adj.p.value
  )

# Step 4: Print ANOVA Table (excluding "ANOVA" column)
anova_table_clean <- anova_table %>%
  select(-ANOVA) # Remove the "ANOVA" column

kable(anova_table_clean, format = "html", caption = "ANOVA Results for Plant Weight by Date") %>%
  kable_styling(bootstrap_options = c("striped", "hover", "condensed"), full_width = F)
ANOVA Results for Plant Weight by Date
Date Term Degrees_of_Freedom Sum_Squares Mean_Square F_Value P_Value
weight_27_days as.factor(N) 3 102657.22 34219.0740 160.4991 0
weight_27_days Residuals 188 40082.39 213.2042 NA NA
weight_34_days as.factor(N) 3 212744.41 70914.8045 107.5439 0
weight_34_days Residuals 188 123967.82 659.4033 NA NA
# Step 5: Print Tukey Post-Hoc Results Table
kable(tukey_table, format = "html", caption = "Tukey Post-Hoc Test Results for Plant Weight by Date") %>%
  kable_styling(bootstrap_options = c("striped", "hover", "condensed"), full_width = F)
Tukey Post-Hoc Test Results for Plant Weight by Date
Date Comparison contrast null.value Mean_Difference Lower_CI Upper_CI Adjusted_p_value
weight_27_days as.factor(N) 150-100 0 7.864583 0.1386026 15.59056 0.0442977
weight_27_days as.factor(N) 200-100 0 29.987500 22.2615192 37.71348 0.0000000
weight_27_days as.factor(N) 50-100 0 -34.360417 -42.0863974 -26.63444 0.0000000
weight_27_days as.factor(N) 200-150 0 22.122917 14.3969359 29.84890 0.0000000
weight_27_days as.factor(N) 50-150 0 -42.225000 -49.9509808 -34.49902 0.0000000
weight_27_days as.factor(N) 50-200 0 -64.347917 -72.0738974 -56.62194 0.0000000
weight_34_days as.factor(N) 150-100 0 6.485417 -7.1018252 20.07266 0.6039725
weight_34_days as.factor(N) 200-100 0 36.502083 22.9148415 50.08933 0.0000000
weight_34_days as.factor(N) 50-100 0 -55.658333 -69.2455752 -42.07109 0.0000000
weight_34_days as.factor(N) 200-150 0 30.016667 16.4294248 43.60391 0.0000002
weight_34_days as.factor(N) 50-150 0 -62.143750 -75.7309919 -48.55651 0.0000000
weight_34_days as.factor(N) 50-200 0 -92.160417 -105.7476585 -78.57317 0.0000000

27 days

# Filter data for 27 days post-planting
df_27 <- df %>% filter(date == "weight_27_days")

# Perform ANOVA and Tukey Post-Hoc Test
anova_27 <- aov(weight ~ as.factor(N), data = df_27)
tukey_results <- TukeyHSD(anova_27)

# Extract Tukey results for the factor 'N'
tukey_pvals <- tukey_results$`as.factor(N)`[, "p adj"]

# Generate group letters based on p-values
group_letters <- multcompLetters(tukey_pvals, threshold = 0.05)$Letters

# Create a data frame of group letters
group_letters_df <- data.frame(
  N = as.numeric(names(group_letters)),
  Label = group_letters
)

# Create the Box Plot with Letters
plot_27 <-df_27 %>%
  ggplot(aes(x = as.numeric(as.character(N)), y = weight, color = as.factor(N))) +
  geom_boxplot(show.legend = FALSE) +
  geom_text(
    data = group_letters_df,
    aes(x = N, y = max(df_27$weight) * 1.05, label = Label),
    inherit.aes = FALSE
  ) +
  scale_x_continuous(breaks = c(50, 100, 150, 200)) +
  labs(title = "Plant Weight (27 Days post planting)",
x = "N Concentration (mg/L)", y = "Plant Weight (gr)", color = "N") +
  theme_minimal()

34 days

## for 34 days:
# Filter data for 34 days post-planting
df_34 <- df %>% filter(date == "weight_34_days")

# Perform ANOVA and Tukey Post-Hoc Test
anova_34 <- aov(weight ~ as.factor(N), data = df_34)
tukey_results_34 <- TukeyHSD(anova_34)

# Extract Tukey results for the factor 'N'
tukey_pvals_34 <- tukey_results_34$`as.factor(N)`[, "p adj"]

# Generate group letters based on p-values
group_letters_34 <- multcompLetters(tukey_pvals_34, threshold = 0.05)$Letters

# Create a data frame of group letters
group_letters_df_34 <- data.frame(
  N = as.numeric(names(group_letters_34)),
  Label = group_letters_34
)

# Create the Box Plot for 34 Days with Letters
plot_34 <- df_34 %>%
  ggplot(aes(x = as.numeric(as.character(N)), y = weight, color = as.factor(N))) +
  geom_boxplot(show.legend = FALSE) +
  geom_text(
    data = group_letters_df_34,
    aes(x = N, y = max(df_34$weight) * 1.05, label = Label),
    inherit.aes = FALSE
  ) +
  scale_x_continuous(breaks = c(50, 100, 150, 200)) +
  labs(
    title = "Plant Weight (34 Days post planting)",
    x = "N Concentration (mg/L)",
    y = "Plant Weight (gr)",
    color = "N"
  ) +
  theme_minimal()
# Render Both Plots in the HTML
grid.arrange(
  plot_27,
  plot_34,
  ncol = 2, # One column for stacking
  heights = c(2, 1.2) # Adjust relative heights of the two plots
)