From 4f48aa0a4c39d062fb63bc98b0c9b231e9c612dd Mon Sep 17 00:00:00 2001 From: Damonsoul <244085168@qq.com> Date: Mon, 4 Nov 2024 10:45:41 +0800 Subject: [PATCH] update desc and example --- man/addcityShape.Rd | 126 ----------------------------------------- man/china_city.Rd | 27 ++++++--- man/china_province.Rd | 27 ++++++--- vignettes/ShapeMap.Rmd | 59 ++++++++++++++----- 4 files changed, 80 insertions(+), 159 deletions(-) delete mode 100644 man/addcityShape.Rd diff --git a/man/addcityShape.Rd b/man/addcityShape.Rd deleted file mode 100644 index 60a7a22..0000000 --- a/man/addcityShape.Rd +++ /dev/null @@ -1,126 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/addCityShape.R -\name{addCityShape} -\alias{addCityShape} -\title{Adds a choropleth map layer for citys with additional customization options.} -\usage{ -addCityShape( - map, - data, - adcode = NULL, - layerId = NULL, - group = NULL, - valueProperty = NULL, - labelProperty = NULL, - labelOptions = leaflet::labelOptions(), - popupProps = NULL, - popupOptions = leaflet::popupOptions(), - scale = c("white", "red"), - steps = 5, - mode = "q", - channelMode = c("rgb", "lab", "hsl", "lch"), - padding = NULL, - correctLightness = FALSE, - bezierInterpolate = FALSE, - colors = NULL, - stroke = TRUE, - color = "#ffffff", - weight = 1, - opacity = 0.5, - fillOpacity = 0.7, - dashArray = NULL, - smoothFactor = 1, - noClip = FALSE, - pathOptions = leaflet::pathOptions(), - highlightOptions = leaflet::highlightOptions(weight = 2, color = "#000000", fillOpacity - = 1, opacity = 1, bringToFront = TRUE, sendToBack = TRUE), - legendOptions = NULL, - ... -) -} -\arguments{ -\item{map}{The leaflet map object to add the layer to.} - -\item{data}{A data frame containing the data to be visualized.} - -\item{adcode}{China administrative division code} - -\item{layerId}{An optional string to identify the layer.} - -\item{group}{An optional string for grouping data.} - -\item{valueProperty}{The property in the geojson data that corresponds to the value to be mapped.} - -\item{labelProperty}{The property in the geojson data that will be used for labels.} - -\item{labelOptions}{Options for labels, defaults to leaflet's labelOptions.} - -\item{popupProps}{A named vector of properties to display in the popup.} - -\item{popupOptions}{Options for popups, defaults to leaflet's popupOptions.} - -\item{scale}{A vector of colors to use for the scale of the choropleth map.} - -\item{steps}{The number of steps for the color scale.} - -\item{mode}{The mode for the color scale, can be "q" for quantile, "e" for equal interval, etc.} - -\item{channelMode}{The color channel mode, can be "rgb", "lab", "hsl", or "lch".} - -\item{padding}{Optional padding for the choropleth layer.} - -\item{correctLightness}{A logical value to correct lightness for color scales.} - -\item{bezierInterpolate}{Whether to use bezier interpolation for the lines.} - -\item{colors}{An optional vector of colors to override the default color scale.} - -\item{stroke}{Whether to draw the stroke along the paths.} - -\item{color}{The color for the paths, defaults to white.} - -\item{weight}{The weight for the paths.} - -\item{opacity}{The opacity for the paths.} - -\item{fillOpacity}{The fill opacity for the paths.} - -\item{dashArray}{An optional array to create dashed lines.} - -\item{smoothFactor}{A factor to smooth the factor for the paths.} - -\item{noClip}{Whether to disable clipping of the paths.} - -\item{pathOptions}{Additional options for the paths, defaults to leaflet's pathOptions.} - -\item{highlightOptions}{Options for highlighting features, defaults to leaflet's highlightOptions.} - -\item{legendOptions}{Options for the legend.} - -\item{...}{Additional arguments passed to other functions.} -} -\value{ -The modified leaflet map object with the added layer. -} -\description{ -Adds a choropleth map layer for citys with additional customization options. -} -\examples{ - - - -# use adcode,adcode can be obtained from leafletZH::china_city -library(leaflet) -library(leaflet.extras) -library(leafletZH) -library(sf) -data <- data.frame(adcode = seq(110101, 110110, 1), value = runif(5)) -leaflet() |> - leafletZH::addTilesAmap() |> - addCityShape( - data = data, adcode = "adcode", valueProperty = "value", - popupProps = c("value") - ) |> - setView(lng = 116, lat = 40, zoom = 8) - -} diff --git a/man/china_city.Rd b/man/china_city.Rd index 0b8a4af..f639f47 100644 --- a/man/china_city.Rd +++ b/man/china_city.Rd @@ -3,21 +3,30 @@ \docType{data} \name{china_city} \alias{china_city} -\title{city} +\title{city data for China} \format{ An object of class \code{sf} (inherits from \code{data.frame}) with 476 rows and 10 columns. } +\source{ +http://datav.aliyun.com/tools/atlas/ + +\describe{ + \item{name}{The name of the district } + \item{adcode}{The administrative code for the district, a unique identifier (e.g., "110101")} + \item{childrenNum}{The number of lower-level administrative divisions within the district (usually 0 for districts)} + \item{level}{The administrative level of the area, which is "district" for all entries in this dataset} + \item{subFeatureIndex}{An index representing the sub-features within the district} + \item{centroid}{The geographical centroid of the district, represented as a string of coordinates } + \item{center}{The center point of the district, also represented as a string of coordinates} + \item{parent}{A JSON string representing the parent administrative entity, usually the province-level data} + \item{acroutes}{A JSON array of administrative codes that represent the full administrative hierarchy leading to the district} + \item{geometry}{Spatial geometry of the district, stored as an sf object in MULTIPOLYGON format} +} +} \usage{ china_city } \description{ -city data for china -source: http://datav.aliyun.com/tools/atlas/ - -\describe{ - \item{name}{city name} - \item{adcode}{adcode} - ... -} +This dataset contains spatial (sf) data for city in China, with various attributes specific to each district. } \keyword{datasets} diff --git a/man/china_province.Rd b/man/china_province.Rd index 8733aed..97bfc74 100644 --- a/man/china_province.Rd +++ b/man/china_province.Rd @@ -3,21 +3,30 @@ \docType{data} \name{china_province} \alias{china_province} -\title{province} +\title{province data for China} \format{ An object of class \code{sf} (inherits from \code{data.frame}) with 34 rows and 10 columns. } +\source{ +http://datav.aliyun.com/tools/atlas/ + +\describe{ + \item{name}{The name of the province } + \item{adcode}{The administrative code for the province, a unique identifier (e.g., "110000")} + \item{childrenNum}{The number of administrative divisions (e.g., counties) within the province} + \item{level}{The administrative level of the area, which is generally "province" for the entries in this dataset} + \item{subFeatureIndex}{An index representing the sub-features within the province} + \item{centroid}{The geographical centroid of the province, represented as a string of coordinates } + \item{center}{The center point of the province, also represented as a string of coordinates} + \item{parent}{A JSON string representing the parent administrative entity, usually the country-level data} + \item{acroutes}{A JSON array of administrative codes that represent the administrative hierarchy leading to the province} + \item{geometry}{Spatial geometry of the province, stored as an sf object in MULTIPOLYGON format} +} +} \usage{ china_province } \description{ -province data for china -source: http://datav.aliyun.com/tools/atlas/ - -\describe{ - \item{name}{province name} - \item{adcode}{adcode} - ... -} +This dataset contains spatial (sf) data for provinces in China, including various attributes related to each province. } \keyword{datasets} diff --git a/vignettes/ShapeMap.Rmd b/vignettes/ShapeMap.Rmd index a1b133c..842432c 100644 --- a/vignettes/ShapeMap.Rmd +++ b/vignettes/ShapeMap.Rmd @@ -18,7 +18,7 @@ knitr::opts_chunk$set( ### Overview -This package provides functions to add choropleth map layers for Chinese cities and provinces to a Leaflet map object. These layers can be customized with various options such as color scales, labels, and popups. +This package provides functions to add shape map layers for Chinese cities and provinces to a Leaflet map object. These layers can be customized with various options such as color scales, labels, and popups. ### Installation @@ -29,11 +29,19 @@ This package provides functions to add choropleth map layers for Chinese cities ### Usage -#### Adding +#### Adding Amap Tiles + +```{r} +library(leaflet) +library(leafletZH) +leaflet() %>% + addTilesAmap() %>% + setView(lng = 120.33739, lat = 31.13533, zoom = 3) +``` #### Adding a City Layer -To add a choropleth map layer for cities, use the addCityShape function. You will need a data frame containing the data to be visualized, including the Chinese administrative division codes (adcode). +To add a shape map layer for cities, use the addCityShape function. You will need a data frame containing the data to be visualized, including the Chinese administrative division codes (adcode).You can get adcode from `leafletZH::china_city` ```{r} library(leaflet) @@ -52,36 +60,57 @@ leaflet() |> #### Adding a Province Layer -To add a choropleth map layer for provinces, use the addProvinceShape function. Similar to the city layer, you will need a data frame containing the data to be visualized. +To add a shape map layer for provinces, use the addProvinceShape function. Similar to the city layer, you will need a data frame containing the data to be visualized.You can get adcode from `leafletZH::china_province` + + ```{r} library(leaflet) library(leaflet.extras) library(leafletZH) -data <- data.frame(name = leafletZH::china_province$name, value = runif(34)) -backg <- htmltools::tags$style(".leaflet-container { background: #000; }") +library(sf) +data <- data.frame(adcode = seq(110000, 150000, 10000), value = runif(5)) leaflet() |> + leafletZH::addTilesAmap() |> addProvinceShape( - data = data, provinceName = "name", valueProperty = "value", + data = data, adcode = "adcode", valueProperty = "value", popupProps = c("value") ) |> - setView(lng = 110, lat = 40, zoom = 4) |> - htmlwidgets::prependContent(backg) + setView(lng = 110, lat = 40, zoom = 3) ``` + +```{r} +library(leaflet) +library(leaflet.extras) +library(leafletZH) +library(dplyr) +data <- data.frame(name = c("河北省", "山西", "陕西"), value = runif(3)) +leaflet() %>% + leafletZH::addTilesAmap() %>% + addProvinceShape( + data = data, + provinceName = "name", + valueProperty = "value", + popupProps = c("value") + ) %>% + setView(lng = 110, lat = 40, zoom = 4) +``` + +Change the background color with `htmlwidgets` + ```{r} library(leaflet) library(leaflet.extras) library(leafletZH) -library(sf) -data <- data.frame(adcode = seq(110000, 150000, 10000), value = runif(5)) +data <- data.frame(name = leafletZH::china_province$name, value = runif(34)) +backg <- htmltools::tags$style(".leaflet-container { background: #000; }") leaflet() |> - leafletZH::addTilesAmap() |> addProvinceShape( - data = data, adcode = "adcode", valueProperty = "value", + data = data, provinceName = "name", valueProperty = "value", popupProps = c("value") ) |> - setView(lng = 110, lat = 40, zoom = 4) + setView(lng = 110, lat = 40, zoom = 2) |> + htmlwidgets::prependContent(backg) ``` -