cnmap: Load China Map on the Levels of Nation, Province, City or...

Description Usage Arguments Details Value Note See Also Examples

View source: R/cnmap.R

Description

Load the China map and do tiny modifications. Unlike the China map in packages maps and mapdata, the dataset in this package is based on up-to-date Chinese official data. And unlike some public data sources such as GADM, the dataset in this package is compliant to Chinese official requirements. cnmap0, cnmap1, cnmap2, cnmap3 are convenient wrappers for level 0 (nation), 1 (province), 2 (city/prefecture), 3 (county) respectively.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
cnmap(level = c("nation", "province", "city", "county"),
  regions = NULL, simplify_level = 1, drop_fragment = FALSE,
  preserve_topo = c("all", "major", "none"), fragment_area = 0.003,
  output = c("spdf", "map", "df", "sf"))

cnmap0(regions = NULL, simplify_level = 1, drop_fragment = FALSE,
  preserve_topo = c("all", "major", "none"), fragment_area = 0.003,
  output = c("spdf", "map", "df", "sf"))

cnmap1(regions = NULL, simplify_level = 1, drop_fragment = FALSE,
  preserve_topo = c("all", "major", "none"), fragment_area = 0.003,
  output = c("spdf", "map", "df", "sf"))

cnmap2(regions = NULL, simplify_level = 1, drop_fragment = FALSE,
  preserve_topo = c("all", "major", "none"), fragment_area = 0.003,
  output = c("spdf", "map", "df", "sf"))

cnmap3(regions = NULL, simplify_level = 1, drop_fragment = FALSE,
  preserve_topo = c("all", "major", "none"), fragment_area = 0.003,
  output = c("spdf", "map", "df", "sf"))

Arguments

level

Character or integer, "nation" (0), "province" (1), "city" (2) or "county" (3). Default 'nation'.

regions

Character strings that fully/partially match ADCODE, NAME, NAME_LAB or NAME_EN fields. E.g., 'beijing' or '^110' to filter the map to Beijing area (case insensitive). It supports regular expression. Default NULL, indicating that all the regions will be shown.

simplify_level

Numeric (0 ~ 1), the proportion of points to retain when applying polygon simplification. See rmapshaper::ms_simplify. If set 1, then the function will load maps with highest border precision (bigger in size). It is useful to reduce the object size when drawing a city- or county-level map by 50%-95%. Default 1 (no simplification).
Be careful to balance the trade-off of simplify_level and plot rendering. The smaller simplify_level is, the longer it takes to simplify the polygons, but the shorter it takes to render the plot.

drop_fragment

Logical, whether drop small fragments in the map (typically small islands). It is useful to reduce the object size by around 10%. Default FALSE.
When trying to drop fragments, it will

  1. at least preserve the largest polygon within a single Polygon slot;

  2. at least preserve the largest polygon with duplicated ADCODE.

preserve_topo

Character ("all", "major", "none"), indicating how to preserve the topology when applying polygon simplification algorithm (only effective when simplify_level < 1). It is highly recommended to set preserve_topo == "all" or "major" to avoid unexpected deformation. Default "all".

  • all: preserve topology of all the polygons, i.e. each Polygons slot and its subordinate Polygon slots are retained.

  • major: only preserve topology of the Polygons slot. Some of the Polygon slots inside the Polygons may be lost.

  • none: do not try to preserve topology, i.e., some of the Polygons and/or Polygon slots inside them will be lost.

fragment_area

Numeric, the threshold area (in km^2) for "fragment" (only effective when drop_fragment = TRUE). It is not recommended to set this value too high. Default 0.003 (the size of the smallest county in China).

output

Character, "spdf", "map", "df", "sf". Default "spdf" (sp::SpatialPolygonsDataFrame).

Details

You can obtain maps by running the codes in examples.
cnmap.png bjmap.png

Value

By default, an sp::SpatialPolygonsDataFrame object (output == "spdf"). You can further process it by

convert to a maps::map object

maps::SpatialPolygons2map()

convert to a data.frame

ggplot2::fortify()

convert to an sf object

sf::st_as_sf()

Note

Direct vs indirect loading

You can also directly load the map datasets using data(cnmap0), data(cnmap1), data(cnmap2), or data(cnmap3). But it is more recommended to call cnmap() to enjoy the benefits by its default settings.

Map meta data & encoding

Suppose you call cn0 <- data(cnmap0), the data set will show in .GlobalEnv. You can then check its meta data by cn0@data (cn0, as a SpatialPolygonsDataFrame object, is of S4 class) or slot(cn0, "data"). If you have issues with the character encoding, then try to switch to a Chinese locale (Sys.setlocale("LC_CTYPE", "Chs")).

Some key fields in map meta dataset:

  • ADCODE: The six-digit official administrative code

  • NAME: Full name of the administrative area

  • NAME_LAB: Short name of the administrative area

  • NAME_EN: English name of the administrative area

Coordinate reference system

The embeded map datasets are applying WGS84 coordinate system. You can define your own coordinate reference system when plotting a map. China's official recommendations:

  • maps::map() or ggplot2::coord_map: set it with mapproj::mapproject()

    • smaller maps: projection="albers", parameters = c(24, 47)

    • larger maps: projection="azequalarea", orientation = c(30, 105, 0)

  • ggplot2::coord_sf: set it using CRS

    • smaller maps: coord_sf(crs="+init=epsg:4490 +proj=aea +ellps=krass +lon_0=105 +lat_1=25 +lat_2=47")

    • larger maps: coord_sf(crs="+init=epsg:4490 +proj=laea +ellps=GRS80 +lon_0=105 +lat_0=30")

Refer to coordcn to use the parameters directly.

See Also

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
## Not run: 
# to yield the 3-level China map as shown on the top
library(maps)
op <- par(mar=c(0, 0, 0, 0))
map(cnmap(3), col="gray90", lwd=0.2, projection="albers", 
  parameters = c(24, 47))  # cnmap(3) is equal to cnmap3()
map(cnmap(2), col="gray75", lwd=0.5, add=TRUE, projection="albers", 
  parameters = c(24, 47))
map(cnmap(1), col="gray30", lwd=0.8, add=TRUE, projection="albers", 
  parameters = c(24, 47))
map(cnmap(0), col="black", lwd=1, add=TRUE, projection="albers", 
  parameters = c(24, 47))
par(op) 

# to draw the 2-level Beijing map as shown on the top
library(ggplot2)
library(sf)
library(extrafont)
bjmap2 <- st_as_sf(cnmap(2, "^110", simplify_level=0.5))
bjmap3 <- st_as_sf(cnmap(3, "^110", simplify_level=0.5))
ggplot() + 
  geom_sf(data=bjmap3, color="gray50", size=0.5) +
  geom_sf(data=bjmap2, fill="transparent", color="gray5", size=0.8) +
  geom_sf_text(aes(label=NAME_LAB), data=bjmap3, family="Microsoft YaHei") +
  coord_sf(crs=coordcn$large$crs) + theme_minimal()

## End(Not run)

madlogos/asesgeo documentation built on Aug. 9, 2019, 9:53 a.m.