--- title: "L9 - Data Virtualization" format: html editor: visual --- # Load Data ```{r} # getwd() # setwd("coding") require(tidyverse) datadf <- readRDS("../data/chinawq/datadf.rds") ``` # Data view ```{r} skimr::skim(datadf) head(datadf) tail(datadf) names(datadf) summary(datadf) str(datadf) ``` # Plot ```{r} #| warning: false #| message: false # 每月NH4N与pH的相关性散点图 p <- datadf |> dplyr::filter(NH4N < 100) |> dplyr::filter(between(year(date), 2016, 2019)) |> mutate(month = month(date)) |> ggplot(aes(CODMn, NH4N)) + geom_point(shape = 21, size = 0.8, fill = "orange") + geom_smooth(method = "gam", color = "red") + scale_x_log10() + scale_y_log10() + labs( x = "CODMn (mg L-1)", y = "Ammonia (mg L-1)" ) + facet_wrap(~month, scale = "free", ncol = 4) + theme( axis.title.x = ggtext::element_markdown(), axis.title.y = ggtext::element_markdown() ) print(p) plotly::ggplotly(p) ``` ```{r} #| warning: false #| message: false # 每月NH4N与pH的相关性散点图 p <- datadf |> dplyr::filter(NH4N < 100) |> dplyr::filter(between(year(date), 2016, 2019)) |> mutate(month = month(date)) |> ggplot(aes(factor(month), NH4N)) + geom_jitter(size = 0.1, colour = "gray95") + geom_violin(fill = "orange", alpha = 0.3) + scale_y_log10() + labs(x = "Month", y = "Ammonia (mg L-1)") + theme_classic() + theme(axis.title.y = ggtext::element_markdown()) print(p) plotly::ggplotly(p) ``` # Plotly ```{r} p <- datadf |> dplyr::filter(year(date) == 2018) |> ggplot(aes(date, NH4N)) + geom_point() ggsave("L9-1.pdf", width = 4, height = 3) # install.packages("plotly") plotly::ggplotly(p) ``` # Map - sf ```{r} # install.packages("sf") # install.packages("sfext") require(sf) require(sfext) chinawqsf <- sf::read_sf( "../data/chinawq/Monitoring_sites/Monitoring_sites.shp" ) chinawqsf |> ggplot() + geom_sf(shape = 21, size = 1, fill = "orange") chinamapsf <- sf::read_sf("../data/中国省级地图GS(2019)1719号.geojson") ninelinesf <- sf::read_sf("../data/九段线GS(2019)1719号.geojson") chinamapsf <- sf::read_sf( "https://git.drwater.net/course/su2026rwep/raw/branch/PUB/data/中国省级地图GS(2019)1719号.geojson" ) ninelinesf <- sf::read_sf( "https://git.drwater.net/course/su2026rwep/raw/branch/PUB/data/九段线GS(2019)1719号.geojson" ) chinacrs <- "+proj=laea +lat_0=40 +lon_0=104" mapeR::get_chinacrs() chinawqsf |> ggplot() + geom_sf(data = chinamapsf) + geom_sf(data = ninelinesf) + geom_sf(shape = 21, size = 1, fill = "orange") + coord_sf(datum = chinacrs) ``` ```{r} require(mapeR) map_chinese() ``` ```{r} chinamapsf <- sf::read_sf("../data/中国省级地图GS(2019)1719号.geojson") ninelinesf <- sf::read_sf("../data/九段线GS(2019)1719号.geojson") chinamapsf <- sf::read_sf( "https://git.drwater.net/course/su2026rwep/raw/branch/PUB/data/中国省级地图GS(2019)1719号.geojson" ) ninelinesf <- sf::read_sf( "https://git.drwater.net/course/su2026rwep/raw/branch/PUB/data/九段线GS(2019)1719号.geojson" ) chinacrs <- "+proj=laea +lat_0=40 +lon_0=104" # 安装 install.packages("ggspatial") datadf |> sf::st_as_sf(coords = c("lon", "lat"), crs = 4326) |> sf::st_transform(crs = chinacrs) |> dplyr::filter(!is.na(CODMn)) |> ggplot() + geom_sf(data = chinamapsf, aes(fill = CNAME), alpha = 0.2) + geom_sf(data = ninelinesf) + geom_sf(aes(colour = log1p(CODMn))) + labs(x = NULL, y = NULL, fill = NULL, colour = "CODMn") + scale_colour_viridis_c() + ggspatial::annotation_north_arrow(location = "tl") + ggspatial::annotation_scale(location = "bl") + theme(legend.position = "none") mapeR::map_chinese ``` # 具体某个湖泊 ```{r} sf::read_sf("../data/taihu.shp") |> ggplot() + geom_sf() sitesf <- sf::read_sf("~/Desktop/qingcaosha.kml") st_layers("~/Desktop/qingcaosha.kml") sitesf <- sf::read_sf("~/Desktop/qingcaosha.kml") boundsf <- sf::read_sf("~/Desktop/qingcaosha.kml", layer = "qingcaosha") ggplot() + geom_sf(data = boundsf) + geom_sf(data = sitesf) + geom_sf_text(data = sitesf, aes(label = Name), vjust = 1, size = 3) ```