library(knitr)
library(CSLSchem)
library(dplyr)
library(ggplot2)
library(extrafont)
library(stringr)

text_size    <- 12
lakes        <- c("Pleasant", "Long", "Plainfield")
pair_names   <- c("MAGNESIUM TOTAL RECOVERABLE",
                  "CALCIUM TOTAL RECOVERABLE",
                  "CHLORIDE",
                  "SODIUM TOTAL RECOVERABLE")
water_chem   <- CSLSdata::water_chem
water_chem   <- water_chem %>%
                filter(.data$description %in% pair_names,
                       .data$site_type != "deep") %>%
                mutate(result = as.numeric(.data$result)) %>%
                select(.data$date, .data$lake, .data$site_type,
                       .data$description, .data$result)
names <- data.frame(description = pair_names,
                    new_name = c("Mg", "Ca", "Cl", "Na"))
names <- names %>% mutate_all(as.character)
water_chem$description <- as.character(water_chem$description)
for (i in 1:nrow(names)) {
  water_chem$description <- str_replace_all(water_chem$description,
                                            names$description[i],
                                            names$new_name[i])
}
pairs        <- merge(water_chem, water_chem,
                      by = c("date", "lake", "site_type"))
colnames(pairs) <- c("date", "lake", "site_type", "x_param", 
                     "x_val", "y_param", "y_val")

lake <- "Pleasant"
plot_df  <- pairs %>% filter(.data$lake %in% c(!!lake, "Precip"))
ggplot(plot_df, 
       aes(x = x_val, y = y_val, color = site_type)) +
              geom_point(na.rm = TRUE) +
              facet_grid(y_param ~ x_param, 
                         scales = "free",
                         switch = "y") +
              stat_smooth(method = "lm", 
                          se = FALSE, 
                          color = "grey40", 
                          na.rm = TRUE) +
              scale_color_manual(name = "",
                                 breaks = c("precipitation",
                                            "upgradient",
                                            "nogradient",
                                            "downgradient",
                                            "lake"),
                                 labels = c("Precipitation",
                                            "Groundwater Inflow",
                                            "Groundawter No Grad.",
                                            "Groundwater Outflow",
                                            "Lake"),
                                 values = c("#1F78B4",
                                            "#33A02C",
                                            "#B15928",
                                            "#B2DF8A",
                                            "#E31A1C")) +
    labs(x = "", y = "", title = sprintf("%s Lake", lake)) +
    theme_bw() +
    theme(text = element_text(family = "Segoe UI Semilight",
                              size = text_size),
          panel.grid.major = element_blank(),
          panel.grid.minor = element_blank())

lake <- "Long"
plot_df  <- pairs %>% filter(.data$lake %in% c(!!lake, "Precip"))
ggplot(plot_df, 
       aes(x = x_val, y = y_val, color = site_type)) +
              geom_point(na.rm = TRUE) +
              facet_grid(y_param ~ x_param, 
                         scales = "free",
                         switch = "y") +
              stat_smooth(method = "lm", 
                          se = FALSE, 
                          color = "grey40",
                          na.rm = TRUE) +
              scale_color_manual(name = "",
                                 breaks = c("precipitation",
                                            "upgradient",
                                            "nogradient",
                                            "downgradient",
                                            "lake"),
                                 labels = c("Precipitation",
                                            "Groundwater Inflow",
                                            "Groundawter No Grad.",
                                            "Groundwater Outflow",
                                            "Lake"),
                                 values = c("#1F78B4",
                                            "#33A02C",
                                            "#B15928",
                                            "#B2DF8A",
                                            "#E31A1C")) +
    labs(x = "", y = "", title = sprintf("%s Lake", lake)) +
    theme_bw() +
    theme(text = element_text(family = "Segoe UI Semilight",
                              size = text_size),
          panel.grid.major = element_blank(),
          panel.grid.minor = element_blank())

lake <- "Plainfield"
plot_df  <- pairs %>% filter(.data$lake %in% c(!!lake, "Precip"))
ggplot(plot_df, 
       aes(x = x_val, y = y_val, color = site_type)) +
              geom_point(na.rm = TRUE) +
              facet_grid(y_param ~ x_param, 
                         scales = "free",
                         switch = "y") +
              stat_smooth(method = "lm", 
                          se = FALSE, 
                          color = "grey40", 
                          na.rm = TRUE) +
              scale_color_manual(name = "",
                                 breaks = c("precipitation",
                                            "upgradient",
                                            "nogradient",
                                            "downgradient",
                                            "lake"),
                                 labels = c("Precipitation",
                                            "Groundwater Inflow",
                                            "Groundawter No Grad.",
                                            "Groundwater Outflow",
                                            "Lake"),
                                 values = c("#1F78B4",
                                            "#33A02C",
                                            "#B15928",
                                            "#B2DF8A",
                                            "#E31A1C")) +
    labs(x = "", y = "", title = sprintf("%s Lake", lake)) +
    theme_bw() +
    theme(text = element_text(family = "Segoe UI Semilight",
                              size = text_size),
          panel.grid.major = element_blank(),
          panel.grid.minor = element_blank())
text_size    <- 12
lakes        <- c("Pleasant", "Long", "Plainfield")
pair_names   <- c("MAGNESIUM TOTAL RECOVERABLE",
                  "ALKALINITY TOTAL CACO3")
water_chem   <- CSLSdata::water_chem
water_chem   <- water_chem %>%
                filter(.data$description %in% pair_names,
                       !.data$site_type %in% c("deep", "precipitation")) %>%
                mutate(result = as.numeric(.data$result)) %>%
                select(.data$date, .data$lake, .data$site_type,
                       .data$description, .data$result)
pairs        <- merge(water_chem, water_chem,
                      by = c("date", "lake", "site_type"))
colnames(pairs) <- c("date", "lake", "site_type", "y_param", 
                     "y_val", "x_param", "x_val")

# psnt_fit <- lm(y_val ~ 0 + x_val, data = filter(pairs, lake == "Pleasant"))
# long_fit <- lm(y_val ~ 0 + x_val, data = filter(pairs, lake == "Long"))
# pfl_fit  <- lm(y_val ~ 0 + x_val, data = filter(pairs, lake == "Plainfield"))

plot_df <- pairs %>% 
           filter(.data$y_param == "MAGNESIUM TOTAL RECOVERABLE",
                  .data$x_param == "ALKALINITY TOTAL CACO3") 

ggplot(plot_df, 
       aes(x = x_val, y = y_val, color = site_type)) +
              geom_point(na.rm = TRUE) +
              facet_grid(~lake) +
              # geom_abline(intercept = 0, 
              #             slope = line_fit$coefficients[1], 
              #             color = 'black') +
              geom_smooth(method = lm, 
                          se = FALSE, 
                          formula = y ~ 0 + x, 
                          color = "grey40") +
              # stat_smooth(method = "lm", se = FALSE, color = "grey40",na.rm = TRUE) +
              scale_color_manual(name = "",
                                 breaks = c("precipitation",
                                            "upgradient",
                                            "nogradient",
                                            "downgradient",
                                            "lake"),
                                 labels = c("Precipitation",
                                            "Groundwater Inflow",
                                            "Groundawter No Grad.",
                                            "Groundwater Outflow",
                                            "Lake"),
                                 values = c("#1F78B4",
                                            "#33A02C",
                                            "#B15928",
                                            "#B2DF8A",
                                            "#E31A1C")) +
    labs(x = "Alkalinity", y = "Magnesium", title = "") +
    theme_bw() +
    theme(text = element_text(family = "Segoe UI Semilight",
                              size = text_size),
          panel.grid.major = element_blank(),
          panel.grid.minor = element_blank())


WDNR-Water-Use/CSLSchem documentation built on July 3, 2020, 10:51 a.m.