---
title: "addShapeMap"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{ShapeMap}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
warning = FALSE,
message = FALSE
)
```
### Overview
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
```{r}
## devtools::install_github('Damonsoul/leafletZH')
## install.packages('leafletZH')
```
### Usage
#### 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 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)
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)
```
#### Adding a Province Layer
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)
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.
```{r}
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`
```{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; }")
leaflet() |>
addProvinceShape(
data = data, provinceName = "name", valueProperty = "value",
popupProps = c("value")
) |>
setView(lng = 110, lat = 40, zoom = 2) |>
htmlwidgets::prependContent(backg)
```
#### Add Area Polygons
Adds a polygon area to a given map using the specified latitude and longitude coordinates,auto convert coordinate and hull.
```{r}
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)
)
```