diff --git a/coding/L7.qmd b/coding/L7.qmd index ce0fe99..2163bc3 100644 --- a/coding/L7.qmd +++ b/coding/L7.qmd @@ -137,31 +137,147 @@ for (isite in 1:10) { ## purrr::map ```{r} +require(tidyverse) +datadf <- readRDS("../data/chinawq/datadf.rds") +datadf |> + select(1:7) |> + tidyr::nest(wqdf = -c(lon, lat)) |> + slice(1:10) |> + mutate( + m = purrr::map( + wqdf, + \(x) { + lm(formula = DO ~ pH, data = x) + } + ) + ) |> + mutate(slope = purrr::map_dbl(m, \(x) coef(x)[2])) |> + mutate( + p = purrr::map2(wqdf, slope, \(x, y) { + x |> + ggplot(aes(x = pH, y = DO)) + + geom_smooth(method = "lm") + + geom_point() + + ggtitle(paste0("Slope: ", round(y, 2))) + }) + ) |> + pull(p) |> + patchwork::wrap_plots() + + patchwork::plot_layout(ncol = 2) +``` - datadf |> - select(1:7) |> - tidyr::nest(wqdf = -c(lon, lat)) |> - slice(1:10) |> - mutate( - m = purrr::map( - wqdf, - \(x) { - lm(formula = DO ~ pH, data = x) - } - ) - ) |> - mutate(slope = purrr::map_dbl(m, \(x) coef(x)[2])) |> - mutate( - p = purrr::map(wqdf, \(x) { - x |> - ggplot(aes(x = pH, y = DO)) + - geom_smooth(method = "lm") + - geom_point() - }) - ) |> - pull(p) |> - patchwork::wrap_plots() + - patchwork::plot_layout(ncol = 2) + +```{r} +longriverdf <- tibble( + fn = dir( + "../data/LongRiver/data/", + pattern = "csv", + full.names = TRUE, + recursive = TRUE + ) +) |> + mutate(data = purrr::map(fn, readr::read_csv)) + +lang <- "cn" +longriverdf |> + mutate(year = gsub("^.*([0-9)]{4})-.*$", "\\1", fn)) |> + mutate(month = gsub("^.*[0-9)]{4}-([0-9]{2}).*$", "\\1", fn)) |> + mutate(site = gsub("^.*_(.*).csv$", "\\1", fn)) |> + select(-fn) |> + slice(1:10) |> + tidyr::unnest(data) |> + ggplot(aes(x = site, y = z)) + + geom_boxplot(colour = "black", size = 0.8) + + facet_wrap(~month) + + dwfun::theme_sci() +``` + +```{r} +longriverdf |> + mutate(year = gsub("^.*([0-9)]{4})-.*$", "\\1", fn)) |> + mutate(month = gsub("^.*[0-9)]{4}-([0-9]{2}).*$", "\\1", fn)) |> + mutate(site = gsub("^.*_(.*).csv$", "\\1", fn)) |> + select(-fn) |> + slice(1:12) |> + mutate( + p = purrr::map(data, \(x) { + x |> + ggplot(aes(tm, z)) + + geom_point() + }) + ) |> + pull(p) |> + patchwork::wrap_plots() |> + patchwork::plot_layout(ncol = 3) +``` + +```{r} +# datadf +# 不同年份,DO 的平均值, +# x lon +# y lat +# colour/ fill Do mean +# facet: year +datadf |> + dplyr::mutate(year = year(date)) |> + summarize(DO_mean = mean(DO, na.rm = TRUE), .by = c(lon, lat, year)) |> + dplyr::filter(year > 2006) |> + ggplot(aes(x = lon, y = lat)) + + geom_point(aes(fill = DO_mean), shape = 21) + + scale_fill_viridis_c() + + facet_wrap(~year) + + labs(x = "Longitude", y = "Latitude", fill = "DO (mg/L)") + + theme_bw() +``` + + +```{r} +# x 月份 +# y NH4: pH + +p1 <- datadf |> + mutate(month = month(date)) |> + dplyr::filter(NH4N < 100) |> + ggplot(aes(x = month, y = NH4N)) + + geom_boxplot(aes(fill = as.factor(month)), colour = "black", size = 0.8) + + scale_y_log10( + breaks = scales::trans_breaks("log10", function(x) 10^x), + labels = scales::trans_format( + "log10", + scales::math_format(10^.x, format = "%.1f") + ) + ) + + scale_x_continuous(breaks = 1:12, labels = month.abb) + + theme(legend.position = "none") +p2 <- datadf |> + mutate(month = month(date)) |> + ggplot(aes(x = month, y = CODMn)) + + geom_boxplot(aes(fill = as.factor(month)), colour = "black", size = 0.8) + + scale_y_log10( + breaks = scales::trans_breaks("log10", function(x) 10^x), + labels = scales::trans_format( + "log10", + scales::math_format(10^.x, format = "%.1f") + ) + ) + + scale_x_continuous(breaks = 1:12, labels = month.abb) + + theme(legend.position = "none") +p3 <- datadf |> + mutate(month = month(date)) |> + dplyr::filter(DO < 20) |> + ggplot(aes(x = month, y = DO)) + + geom_boxplot(aes(fill = as.factor(month)), colour = "black", size = 0.8) + + scale_x_continuous(breaks = 1:12, labels = month.abb) + + theme(legend.position = "none") +p4 <- datadf |> + mutate(month = month(date)) |> + dplyr::filter(pH > 5) |> + ggplot(aes(x = month, y = pH)) + + geom_boxplot(aes(fill = as.factor(month)), colour = "black", size = 0.8) + + scale_x_continuous(breaks = 1:12, labels = month.abb) + + theme(legend.position = "none") + +(p1 | p2) / (p3 | p4) + patchwork::plot_annotation(tag_levels = "A") ``` diff --git a/data/LongRiver b/data/LongRiver new file mode 160000 index 0000000..d316bf4 --- /dev/null +++ b/data/LongRiver @@ -0,0 +1 @@ +Subproject commit d316bf46568a202cdc320e086403d22d7e303f2d