title: "thematic map"
author: "wintryheart"
date: "2019/8/16"
output:
html_document:
toc: true
number_sections: true
toc_depth: 1
## ----echo=FALSE----------------------------------------------------------
knitr::opts_chunk$set( warning = FALSE, message = FALSE, echo = TRUE)
knitr::knit_hooks$set(margin = function(before, options, envir){
if (before){
par(mar=c(0.1,0.1,1.3,0.1))
}
})
cartography專題地圖包簡介
cartography包要配合sf包或sp包使用倘屹,即队询,cartography包調(diào)用sf或sp格式的空間數(shù)據(jù)摊册,生成專題地圖称簿。
由于cartography包的大部分內(nèi)在組件都依賴sf空間功能,所以首先空間對象是sf格式绰更。
-
cartography包自帶martinique地圖(馬提尼克议蟆,拉丁美洲向風(fēng)群島中部法屬島嶼,首府法蘭西堡)和數(shù)據(jù)歹篓。
- shp格式(shape/martinique.shp)瘫证,
- 地理包gpkg格式(gpkg/mtq.gpkg),包含空間數(shù)據(jù)和專題數(shù)據(jù)庄撮。
- 其他數(shù)據(jù)csv格式(csv/mob.csv)
- 示例文件(doc/cartography.html)
主函數(shù)類別
Symbology系列
每個函數(shù)代表一種專題背捌,例如,比例符號(proportional symbols)或 面量圖(等值線洞斯、地區(qū)分布圖)(choropleth)载萌,然后顯示在地圖上。
允許將每個專題展示作為一個圖層,并在同一地圖上疊加展示扭仁。
-
每個函數(shù)都有兩個主參數(shù):
- x垮衷,空間對象,一般是sf對象乖坠。
- var, 映射的變量名搀突。
-
sp對象可以通過spdf參數(shù)和df參數(shù)處理。
- 如果變量包含進Spatial*DataFrame熊泵,使用spdf參數(shù)仰迁;
- 如果變量在獨立的data.frame,需要并入Spatial*DataFrame顽分,則使用spdf, spdfid, df, dfid參數(shù)徐许。
-
還有很多參數(shù)用于調(diào)節(jié)專題地圖展示效果,這些參數(shù)也是GIS和自動制圖工具中常見的參數(shù)卒蘸。
- classification and color palettes 用于面量圖(choropleth maps)
- symbols sizes 用于比例符號地圖( proportional symbols maps)
專題類型 | 函數(shù) |
---|---|
Choropleth面量圖 | choroLayer(x = mtq, var = "myvar", method = "quantile", nclass = 8) |
Typology類型圖 | typoLayer(x = mtq, var = "myvar") |
Proportional Symbols比例符號圖 | propSymbolsLayer(x = mtq, var = "myvar", inches = 0.1, symbols = "circle") |
Colorized Proportional Symbols (relative data) 彩色的比例符號圖(相關(guān)數(shù)據(jù)) |
propSymbolsChoroLayer(x = mtq, var = "myvar", var2 = "myvar2") |
Colorized Proportional Symbols (qualitative data)彩色的比例符號圖(定性數(shù)據(jù)) | propSymbolsTypoLayer(x = mtq, var = "myvar", var2 = "myvar2") |
Double Proportional Symbols雙比例符號圖 | propTrianglesLayer(x = mtq, var1 = "myvar", var2 = "myvar2") |
OpenStreetMap Basemap (see rosm package) OpenStreetMap基礎(chǔ)圖 |
tiles <- getTiles(x = mtq, type = "osm") tilesLayer(tiles) |
Isopleth (see SpatialPosition package)等值線圖 | smoothLayer(x = mtq, var = "myvar", typefct = "exponential", span = 500, beta = 2) |
Discontinuities斷裂圖 | discLayer(x = mtq.borders, df = mtq, var = "myvar", threshold = 0.5 |
Flowsi流向圖 | gradLinkLayer(x = mob.sf, df = mob, var = "fij",breaks = c(109,500,1000,2000,4679)) propLinkLayer(x = mtq_link, df = mtq_df,var = "fij") |
Dot Density點密度圖 | dotDensityLayer(x = mtq, var = "myvar") |
Labels標(biāo)簽 | labelLayer(x = mtq, txt = "myvar", halo = TRUE, overlap = FALSE) |
library(cartography)
library(sf)
# path to the geopackage file embedded in cartography
path_to_gpkg <- system.file("gpkg/mtq.gpkg", package="cartography")
# import to an sf object
mtq <- st_read(dsn = path_to_gpkg, quiet = TRUE)
# Choropleth
# plot(st_geometry(mtq), col = NA, border = NA, bg = "#aadaff")
choroLayer(x = mtq, var = "POP", method = "quantile", nclass = 8)
# Typology
# plot(st_geometry(mtq), col = NA, border = NA, bg = "#aadaff")
typoLayer(x = mtq, var = "LIBGEO")
# Proportional Symbols
plot(st_geometry(mtq), col = NA, border = NA, bg = "#aadaff")
propSymbolsLayer(x = mtq, var = "POP", inches = 0.1, symbols = "circle")
plot(st_geometry(mtq), lwd = 0.5, border = "grey20", add = TRUE, lty = 3)
# Colorized Proportional Symbols (relative data) 兩個連續(xù)變量
plot(st_geometry(mtq), col = NA, border = NA, bg = "#aadaff")
propSymbolsChoroLayer(x = mtq, var = "POP", var2 = "MED")
plot(st_geometry(mtq), lwd = 0.5, border = "grey20", add = TRUE, lty = 3)
# Colorized Proportional Symbols (qualitative data) 一個連續(xù)一個分類變量
plot(st_geometry(mtq), col = NA, border = NA, bg = "#aadaff")
propSymbolsTypoLayer(x = mtq, var = "POP", var2 = "STATUS")
plot(st_geometry(mtq), lwd = 0.5, border = "grey20", add = TRUE, lty = 3)
# Double Proportional Symbols
plot(st_geometry(mtq), col = NA, border = NA, bg = "#aadaff")
propTrianglesLayer(x = mtq, var1 = "POP", var2 = "MED")
plot(st_geometry(mtq), lwd = 0.5, border = "grey20", add = TRUE, lty = 3)
# OpenStreetMap Basemap (see rosm package)
tiles <- getTiles(x = mtq, type = "osm")
tilesLayer(tiles)
# Isopleth (see SpatialPosition package)
library(SpatialPosition)
smoothLayer(x = mtq, var = "POP", typefct = "exponential", span = 4000, beta = 2, mask = mtq)
typefct參數(shù):空間交互作用函數(shù)(spatial interaction function)雌隅,有兩個選項"pareto" (means power law帕累托冪律分布) or "exponential". 如果是"pareto"分布,交互作用函數(shù)為: (1 + alpha * mDistance) ^ (-beta)缸沃。如果是 "exponential"分布恰起, 交互作用函數(shù)為 : exp(- alpha * mDistance ^ beta) 。alpha參數(shù)由用戶給出的span和beta參數(shù)計算出趾牧。
span參數(shù): 空間相互作用函數(shù)的概率密度為0.5的距離
beta參數(shù): 空間相互作用函數(shù)的阻尼因子
如果有var2,那么將計算var1(分子)和var2(分母)的比率检盼。
對于中國地圖,建議取span=200000,beta=2
# Discontinuities
# Get a SpatialLinesDataFrame of countries borders 獲取邊界線數(shù)據(jù)框
mtq.contig <- getBorders(mtq)
# plot municipalities 畫區(qū)劃圖
plot(st_geometry(mtq))
# Plot the population with custom breaks 畫面量圖
choroLayer(x = mtq, var = "POP", legend.pos = "topright")
# Plot discontinuities畫
discLayer(x = mtq.contig, df = mtq, var = "POP", method = "geom", nclass = 3, threshold = 0.4)
# Flows包含三種:
# gradLinkLayer(): Graduated Links Layer 繪制一個分級鏈接層翘单。鏈接是根據(jù)離散的寬度類別繪制的吨枉。
# gradLinkTypoLayer(): Graduated and Colored Links Layer繪制一個彩色和漸變鏈接層。鏈接是根據(jù)離散類別的寬度繪制的哄芜。顏色依賴于離散變量的類別貌亭。
# propLinkLayer(): Proportional Links Layer繪制一個比例鏈接層。鏈接寬度與變量的值成正比忠烛。
mtq <- st_read(system.file("gpkg/mtq.gpkg", package="cartography"))
mob <- read.csv(system.file("csv/mob.csv", package="cartography"))
# Create a link layer - work mobilities to Fort-de-France (97209)
mob.sf <- getLinkLayer(x = mtq, df = mob[mob$j==97209,], dfid = c("i", "j"))
# Plot the links - Work mobility
plot(st_geometry(mtq), col = "grey60",border = "grey20")
propLinkLayer(x = mob.sf, df = mob, maxlwd = 10, legend.pos = "topright", var = "fij",
col = "#92000090", add = TRUE)
# Dot Density
plot(st_geometry(mtq), col = NA, border = NA, bg = "#aadaff")
dotDensityLayer(x = mtq, var = "POP")
plot(st_geometry(mtq), lwd = 0.5, border = "grey20", add = TRUE, lty = 3)
# Label
labelLayer(x = mtq, txt = "POP",halo = TRUE, overlap = FALSE)
Transformations系列
這組函數(shù)用于創(chuàng)建或轉(zhuǎn)換空間對象属提,例如邊界提取权逗、網(wǎng)格或鏈接創(chuàng)建(borders extraction, grid or links creation)美尸。
提供這些功能是為了方便創(chuàng)建一些通常需要地理處理的更高級的地圖。
類型 | 用法 | 說明 |
---|---|---|
Polygons to Grid | mtq_grid <- getGridLayer(x = mtq, cellsize = 3.6e+07, type = "hexagonal", var = "myvar") |
Grids layers can be used by choroLayer() or propSymbolsLayer(). |
Points to Links | mtq_link <- getLinkLayer(x = mtq, df = link) | Links layers can be used by *LinkLayer(). |
Polygons to Borders | mtq_border <- getBorders(x = mtq) | Borders layers can be used by discLayer() function |
Polygons to Pencil Lines | mtq_pen <- getPencilLayer(x = mtq) |
Map Layout系列
- 配合其它cartography函數(shù)使用斟薇,用于布局設(shè)計(layout design)师坎,例如,自定義比例尺堪滨,指北箭頭胯陋,標(biāo)題,來源或作者信息等( customizable scale bar, north arrow, title, sources or author information…)。
名稱 | 含義 | 函數(shù) |
---|---|---|
North Arrow | 指北箭頭 | north(pos = "topright") |
Scale Bar | 比例尺 | barscale(size = 5) |
Full Layout | 完整布局 | layoutLayer(title = "Martinique", subtitle = TRUE, frame = TRUE, author = "Author", sources = "Sources", north = TRUE, scale = 5) |
Figure Dimensions | 圖的尺寸:基于空間對象的尺寸比例(dimension ratio)遏乔、 圖邊距(figure margins)和輸出分辨率(output resolution) 獲取圖像外形尺寸(figure dimensions)义矛。 |
f_dim <- getFigDim(x = sf_obj, width = 500,mar = c(0,0,0,0)) png("fig.png", width = 500, height = f_dim[2]) par(mar = c(0,0,0,0)) plot(sf_obj, col = "#729fcf") dev.off() |
Color Palettes系列
cartography包封裝16種原色調(diào)色板。這些調(diào)色板可以定制和組合盟萨。
carto.pal(pal1 = "blue.pal", n1 = 5, pal2 = "sand.pal", n2 = 3)
調(diào)色板方案參見cheatsheet凉翻。
1 | 2 | 3 | 4 |
---|---|---|---|
blue.pal | orange.pal | red.pal | brown.pal |
green.pal | pink.pal | purple.pal | wine.pal |
grey.pal | sand.pal | kaki.pal | pastel.pal |
turquoise.pal | taupe.pal | harmo.pal | multi.pal |
Legends系列
默認(rèn)圖例伴隨制圖層顯示。
更多的參數(shù)可以通過legend*()函數(shù)定義捻激。
函數(shù) | 含義 |
---|---|
legendBarsSymbols | Legend for Proportional Bars Maps |
legendChoro | Legend for Choropleth Maps |
legendCirclesSymbols | Legend for Proportional Circles Maps |
legendGradLines | Legend for Graduated Size Lines Maps |
legendPropLines | Legend for Proportional Lines Maps |
legendPropTriangles | Legend for Double Proportional Triangles Maps |
legendSquaresSymbols | Legend for Proportional Squares Maps |
legendTypo | Legend for Typology Maps |
Classification系列
數(shù)據(jù)分類制轰、地圖分級(Classification)通常用于Choropleth Map(面量圖)的制作中,是專題制圖中最初胞谭,也是最基本的一個步驟垃杖。
getBreaks() 用于連續(xù)變量進行分類,允許訪問大多數(shù)用于數(shù)據(jù)綁定的分類方法丈屹。
語法:getBreaks(v, nclass = NULL, method = "quantile", k = 1, middle = FALSE)
-
可用的method包括: quantile, equal, q6, fisher-jenks, mean-sd, sd, geometric progression...
- "sd", "equal", "quantile" and "fisher-jenks" are classIntervals methods.
- Jenks and Fisher-Jenks algorithms are based on the same principle and give quite similar results but Fisher-Jenks is much faster.
- The "q6" method uses the following quantile probabilities: 0, 0.05, 0.275, 0.5, 0.725, 0.95, 1.基于特定的分位數(shù)概率调俘。
- The "geom" method is based on a geometric progression along the variable values. 基于幾何級數(shù)(等比級數(shù))
- The "arith" method is based on an arithmetic progression along the variable values. 基于算術(shù)級數(shù)(等差級數(shù))
- The "em" method is based on nested averages computation. 基于嵌套的平均值計算
- The "msd" method is based on the mean and the standard deviation of a numeric vector. 基于數(shù)值向量的均值和標(biāo)準(zhǔn)差
- The nclass parameter is not relevant, use k and middle instead.
- k indicates the extent of each class in share of standard deviation. 每個類別在標(biāo)準(zhǔn)差中所占份額的程度
- If middle=TRUE then the mean value is the center of a class else the mean is a break value.
- "sd", "equal", "quantile" and "fisher-jenks" are classIntervals methods.
分類方法的詳細(xì)的中文說明參見數(shù)據(jù)分類那些事兒
bks1 <- getBreaks(v = mtq$POP, nclass = 6, method = "quantile")
bks2 <- getBreaks(v = mtq$POP, nclass = 6, method = "fisher-jenks")
pal <- carto.pal("green.pal",3, "wine.pal", 3)
hist(mtq$POP, breaks = bks1, col = pal)
hist(mtq$POP, breaks = bks2, col = pal)
setwd("d:/province")
chinamap <- st_read("province.shp")
#點密度圖
plot(st_geometry(chinamap))
dotDensityLayer(x=chinamap, var="POPU",pch=20, col="red4", n=50)
#平滑數(shù)據(jù)圖
smoothLayer(x = chinamap, var = 'POPU',
span = 200000, beta = 2,
mask = chinamap, border = NA,
col = carto.pal(pal1 = 'wine.pal', n1 = 8),
legend.title.txt = "Population\nPotential",
legend.pos = "topright", legend.values.rnd = 0)
propSymbolsLayer(x = chinamap, var = "POPU", col = NA, border = "#ffffff50")
library(ggplot2)
library(cartography)
library(tidyverse)
older <- read.csv("c:/users/wintryheart/desktop/oldermarriage.csv")
chinamap <- read_sf("d:/province/province.shp")
chinamap2 <- left_join(chinamap, older, by=c("NAME"="province"))
chinamap2 <- select(chinamap2, "NAME","unmar","married","divorce","widow")
choroLayer(x=chinamap2,var="unmar", method = "fisher-jenks", nclass = 4, legend.pos = "right")
layoutLayer(title="中國老年人未婚狀況(2010)",sources = "數(shù)據(jù)來源:中國第六次人口普查", author = "制圖:李亮")
choroLayer(x=chinamap2,var="married", method = "fisher-jenks", nclass = 4, legend.pos = "right")
layoutLayer(title="中國老年人有配偶狀況(2010)",sources = "數(shù)據(jù)來源:中國第六次人口普查", author = "制圖:李亮")
choroLayer(x=chinamap2,var="divorce", method = "fisher-jenks",nclass=4, legend.pos = "right")
layoutLayer(title="中國老年人離婚狀況(2010)",sources = "數(shù)據(jù)來源:中國第六次人口普查", author = "制圖:李亮")
choroLayer(x=chinamap2,var="widow", method = "fisher-jenks",nclass=4, legend.pos = "right")
layoutLayer(title="中國老年人喪偶狀況(2010)",sources = "數(shù)據(jù)來源:中國第六次人口普查", author = "制圖:李亮")