SoilDATA$Month <- factor(SoilDATA$Month, levels = month.name)

SoilLong <- SoilDATA %>%
  pivot_longer(
    cols = c("Nitrate","Nitratelbs","P","AmmoniumN","Ammoniumlbs","TotalN","TotalC"),
    names_to = "Nutrient",
    #names_prefix = "wk",
    values_to = "Nutr-val",
    values_drop_na = FALSE
  )

Nutr_mean <- SoilLong %>%
  group_by(Month, Severity, Nutrient) %>%
  summarise(
    nut_mn = mean(`Nutr-val`, na.rm = TRUE),
    #calculates the mean by group
    nut_sd = sd(`Nutr-val`, na.rm = TRUE),
    #calculates the standard deviation by group
    count = n(),
    #counts the observations used to make mean and sd
    nut_se = nut_sd/ sqrt(count)
  ) %>%   #mutate takes the previous data and calculates the standard error
  ungroup()
## `summarise()` has grouped output by 'Month', 'Severity'. You can override using
## the `.groups` argument.
#filtering out important nutrient values
Nutdat <- Nutr_mean %>% filter(Nutrient=="AmmoniumN" | Nutrient=="Nitrate" | Nutrient== "P"| Nutrient=="TotalC" | Nutrient=="TotalN")


# Using facet grid to create grid of all plots 
Nutgraph <- ggplot(Nutdat, aes(x=`Severity`, y=nut_mn, color=`Month`)) + 
  geom_line() +
  geom_point()+
  geom_errorbar(aes(ymin=nut_mn-nut_se, ymax=nut_mn+nut_se), width=.2,
                position=position_dodge(0.05)) +
  labs(title="Nutrient vs Fire", x="Severity", y = "MeanSE+-")+
  theme_bw() #+


Nut_grid <- Nutgraph + facet_wrap(~Nutrient, ncol = 3, scales = "free")
print(Nut_grid)
## `geom_line()`: Each group consists of only one observation.
## ℹ Do you need to adjust the group aesthetic?
## `geom_line()`: Each group consists of only one observation.
## ℹ Do you need to adjust the group aesthetic?
## `geom_line()`: Each group consists of only one observation.
## ℹ Do you need to adjust the group aesthetic?
## `geom_line()`: Each group consists of only one observation.
## ℹ Do you need to adjust the group aesthetic?
## `geom_line()`: Each group consists of only one observation.
## ℹ Do you need to adjust the group aesthetic?