drawChinaMap: Draw standard China map.

View source: R/drawChinaMap.R

drawChinaMapR Documentation

Draw standard China map.

Description

Draw standard China map.

Usage

drawChinaMap(
  provdata = NULL,
  citydata = NULL,
  var.prov = "prov",
  var = NA,
  multiply = 1,
  log = FALSE,
  digits = 0,
  nsmall = digits,
  colors = "Blues",
  direc = 1,
  cityshape = 18,
  cityalpha = 0.9,
  addlabel = TRUE,
  labelprefix = "",
  labelseg = ": ",
  tag = "",
  title = var,
  subtitle = "",
  guidetitle = "",
  addguidelabel = TRUE,
  guidelimits = NULL,
  guidebreaks = NULL,
  guidelabels = NULL,
  bordersize = 0.2,
  bordercolor = "grey70",
  na.color = "grey90",
  font = NULL,
  filename = "ChinaMap.png",
  dpi = 500
)

Arguments

provdata

Province-level data. You can use left_join or right_join to merge your province-level data with provdata_temp (see Examples).

citydata

City-level data with two variables (must be "geoE" and "geoN") specifying the longitude and latitude of cities, respectively.

var.prov

The variable of provinces, e.g., "prov".

var

The variable to show on the map.

multiply

A number useful when you want to expand the raw values by, e.g., 100 times.

log

TRUE or FALSE (default). Whether to log-transform the raw values.

digits, nsmall

Number of decimal places of output. Defaults to 0.

colors

Color palettes. The following palettes are available (see scale_color_brewer):

Sequential: Blues, Greens, Greys, Oranges, Purples, Reds, BuGn, BuPu, GnBu, OrRd, PuBu, PuRd, RdPu, YlGn, PuBuGn, YlGnBu, YlOrBr, YlOrRd

Diverging: BrBG, PiYG, PRGn, PuOr, RdBu, RdGy, RdYlBu, RdYlGn, Spectral

Qualitative (not suggested): Accent, Dark2, Paired, Pastel1, Pastel2, Set1, Set2, Set3

direc

1 (default) or -1, specifying the direction of color palette.

cityshape

The shape of city dots. 16 (round) or 18 (rhombus) would be fine. Defaults to 18. See shape parameter.

cityalpha

The transparency of city dots. Defaults to 0.9.

addlabel

TRUE (default) or FALSE. Whether to add value labels. For clarity, value labels are only added to provinces but not to cities.

labelprefix

A character specifying a variable in your data for adding label prefix, usually "prov" if you want to add the names of provinces prior to values. (Note: You can draw the label prefix only, by setting addlable=FALSE and labelprefix="yourvariable".)

labelseg

A character specifying the joint character between label prefix and values (e.g., setting to ": " will make a label look like "Beijing: 1.23").

tag

Tag of the map (left-top corner). Defaults to "", which leaves the position occupied without any text.

title

Title of the map. Defaults to the variable name.

subtitle

Subtitle of the map. Defaults to NULL, which does not occupy the position.

guidetitle

Title of the colorbar guide.

addguidelabel

TRUE (default) or FALSE. Whether to add values under the colorbar guide.

guidelimits

A number vector specifying the range of values to plot (relevant both to the main plot and to the colorbar guide). Defaults to the actual range of your variable.

guidebreaks

A number vector specifying the breaking points of colorbar, e.g., seq(0, 100, 25).

guidelabels

A vector re-setting the labels of the colorbar guide.

bordersize

Line size of map border. Defaults to 0.2.

bordercolor

Line color of map border. Defaults to "grey70".

na.color

A color for those provinces with missing values. Defaults to "grey90".

font

Text font. Only for Windows systems.

filename

File name to be saved, should be either ".png" or ".jpg".

dpi

Dots per inch (DPI). Defaults to 500.

Value

Invisibly return a list of two maps (a main map and a sub-map for Nanhai islands).

Examples

## Template
# View(provdata_temp)  # a template province-level dataset
drawChinaMap()  # draw a template of China map (no variables)

drawChinaMap(provdata_temp,
             var="geoE", digits=1,
             filename="ChinaMap1.png")
drawChinaMap(provdata_temp,
             var="geoN", digits=1,
             colors="Reds", direc=-1, addlabel=FALSE,
             filename="ChinaMap2.png")

## How to use it with real data?
# View(provdata_demo)  # a demo dataset (per capita GDP for 31 mainland provinces)

# Method 1: Use the 'var.prov' parameter
drawChinaMap(provdata_demo, var.prov="Province",
             var="GDPpc", digits=0,
             filename="ChinaMap_GDPpc.png")

# Method 2: Use dplyr::left_join() or dplyr::right_join() to merge datasets
provdata = dplyr::right_join(provdata_temp, provdata_demo, by=c("prov"="Province"))
drawChinaMap(provdata,
             var="GDPpc", digits=0,
             title="GDP per capita",
             filename="ChinaMap_GDPpc.png")

# delete files for code check
unlink(c("ChinaMap.png",
         "ChinaMap1.png",
         "ChinaMap2.png",
         "ChinaMap_GDPpc.png"))


psychbruce/drawMap documentation built on Nov. 20, 2022, 1:24 a.m.