knitr::opts_chunk$set( collapse = TRUE, warning = FALSE, message = FALSE )
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.
## devtools::install_github('Damonsoul/leafletZH') ## install.packages('leafletZH')
library(leaflet) library(leafletZH) leaflet() |> addTilesAmap() |> setView(lng = 120.33739, lat = 31.13533, zoom = 3)
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
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)
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
library(leaflet) library(leaflet.extras) library(leafletZH) library(sf) data <- data.frame(adcode = seq(110000, 150000, 10000), value = runif(5)) leaflet() |> leafletZH::addTilesAmap() |> addProvinceShape( data = data, adcode = "adcode", valueProperty = "value", popupProps = c("value") ) |> setView(lng = 110, lat = 40, zoom = 3)
You don't need then full name of province, addProvinceShape
only use the first two word of province name to match.
library(leaflet) library(leaflet.extras) library(leafletZH) 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
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; }") leaflet() |> addProvinceShape( data = data, provinceName = "name", valueProperty = "value", popupProps = c("value") ) |> setView(lng = 110, lat = 40, zoom = 2) |> htmlwidgets::prependContent(backg)
Adds a polygon area to a given map using the specified latitude and longitude coordinates,auto convert coordinate and hull.
library(leaflet) library(leafletZH) leaflet() |> addTilesAmap() |> addAreaPolygons( longitude = c(121.0, 122.1, 121.2, 122.15, 121.5), latitude = c(31.1, 31.919, 31.917, 31.15, 31.5), coordinate = "WGS-84" ) |> addAwesomeMarkers( lng = c(121.0, 122.1, 121.2, 122.15, 121.5), lat = c(31.1, 31.919, 31.917, 31.15, 31.5) )
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.