Mapping China and Its provinces, Municipalities and Autonomous Regions

Installation

You can install this package from CRAN or GitHub:

# From CRAN
install.packages('hchinamap', build_vignettes = TRUE)
# From  GitHub
devtools::install_github('czxa/hchinamap', build_vignettes = TRUE)
# or use git
devtools::install_git("https://github.com/czxa/hchinamap.git", build_vignettes = TRUE)

Usage

I made a demo datasets for this package. You can load it by following method:

dir <- tempdir()
download.file('https://czxb.github.io/br/chinadf.rda', file.path(dir, 'chinadf.rda'))
load(file.path(dir, 'chinadf.rda'), verbose = TRUE)
#> Loading objects:
#>   chinadf

Additional annotations to parameters

  • region parameter indicate which map to use, you can choose like “China”, “Anhui”, … “Macao” and so on. Because the map data of Taiwan have not been collated yet, it is impossible to draw provincial map of Taiwan Province for the time being.
  • name parameter should be in Chinese. like c(“北京”, “天津”, “上海”).

Mapping China:

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(magrittr)
library(hchinamap)
china <- chinadf %>% 
  dplyr::filter(region == "China")
hchinamap(name = china$name, value = china$value,
           width = "100%", height = "400px",
           title = "Map of China", region = "China")

Modify the maximum and minimum colors:

china <- chinadf %>%
  dplyr::filter(region == "China")
hchinamap(name = china$name, value = china$value,
          region = "China",
           width = "100%", height = "400px",
           title = "Map of China",
           minColor = "#f1eef6",
           maxColor = "#980043")

Mapping provinces

For example, map of Anhui:

anhui <- chinadf %>%
  dplyr::filter(region == "Anhui")
hchinamap(name = anhui$name, value = anhui$value,
           width = "100%", height = "500px",
           title = "Map of Anhui", region = "Anhui")

Map of Guangdong:

gd <- chinadf %>%
  dplyr::filter(region == "Guangdong")
hchinamap(name = gd$name, value = gd$value,
           width = "100%", height = "400px",
           title = "Map of Guangdong", region = "Guangdong")

Map of Heilongjiang:

hlj <- chinadf %>%
  dplyr::filter(region == "Heilongjiang")
hchinamap(name = hlj$name, value = hlj$value,
           width = "100%", height = "400px",
           title = "Map of Heilongjiang", region = "Heilongjiang")

Theme

If there are multiple map charts created by this package in a RMarkdown document, use topic parameters carefully, because the charts may interfere with each other.

sx <- chinadf %>%
  dplyr::filter(region == "Shaanxi")
hchinamap(name = sx$name, value = sx$value,
           width = "100%", height = "400px",
           title = "Map of Shaanxi", region = "Shaanxi",
           theme = "darkunica",
           titleColor = "white")

Use it in ‘RMarkdown’ and ‘Shiny’ Apps

Here if a very simple ‘shiny’ example:

dir <- system.file("examples", "hchinamap", package = "hchinamap")
setwd(dir)
shiny::shinyAppDir(".")

License

MIT © czxa.top