cartography 主題地圖


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)
1.png
# Typology
# plot(st_geometry(mtq), col = NA, border = NA, bg = "#aadaff")
typoLayer(x = mtq, var = "LIBGEO")
2.png
# 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)
3.png
# 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)
4.png
# 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)
5.png
# 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)
6.png
# OpenStreetMap Basemap (see rosm package) 
tiles <- getTiles(x = mtq, type = "osm")
tilesLayer(tiles)
7.png
# Isopleth (see SpatialPosition package)
library(SpatialPosition)
smoothLayer(x = mtq, var = "POP", typefct = "exponential", span = 4000, beta = 2, mask = mtq)
8.png

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)
9.png

10.png
# 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)
11.png
# 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)

12.png

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.
  • 分類方法的詳細(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)  
13.png
14.png
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")
15.png
16.png
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 = "制圖:李亮")

17.png
18.png
19.png
20.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市泉瞻,隨后出現(xiàn)的幾起案子脉漏,更是在濱河造成了極大的恐慌,老刑警劉巖袖牙,帶你破解...
    沈念sama閱讀 217,907評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件侧巨,死亡現(xiàn)場離奇詭異,居然都是意外死亡鞭达,警方通過查閱死者的電腦和手機司忱,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,987評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來畴蹭,“玉大人坦仍,你說我怎么就攤上這事∵督螅” “怎么了繁扎?”我有些...
    開封第一講書人閱讀 164,298評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長糊闽。 經(jīng)常有香客問我梳玫,道長,這世上最難降的妖魔是什么右犹? 我笑而不...
    開封第一講書人閱讀 58,586評論 1 293
  • 正文 為了忘掉前任提澎,我火速辦了婚禮,結(jié)果婚禮上念链,老公的妹妹穿的比我還像新娘盼忌。我一直安慰自己积糯,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,633評論 6 392
  • 文/花漫 我一把揭開白布谦纱。 她就那樣靜靜地躺著看成,像睡著了一般。 火紅的嫁衣襯著肌膚如雪跨嘉。 梳的紋絲不亂的頭發(fā)上绍昂,一...
    開封第一講書人閱讀 51,488評論 1 302
  • 那天,我揣著相機與錄音偿荷,去河邊找鬼窘游。 笑死,一個胖子當(dāng)著我的面吹牛跳纳,可吹牛的內(nèi)容都是我干的忍饰。 我是一名探鬼主播,決...
    沈念sama閱讀 40,275評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼寺庄,長吁一口氣:“原來是場噩夢啊……” “哼艾蓝!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起斗塘,我...
    開封第一講書人閱讀 39,176評論 0 276
  • 序言:老撾萬榮一對情侶失蹤赢织,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后馍盟,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體于置,經(jīng)...
    沈念sama閱讀 45,619評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,819評論 3 336
  • 正文 我和宋清朗相戀三年贞岭,在試婚紗的時候發(fā)現(xiàn)自己被綠了八毯。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,932評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡瞄桨,死狀恐怖话速,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情芯侥,我是刑警寧澤泊交,帶...
    沈念sama閱讀 35,655評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站柱查,受9級特大地震影響廓俭,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜物赶,卻給世界環(huán)境...
    茶點故事閱讀 41,265評論 3 329
  • 文/蒙蒙 一白指、第九天 我趴在偏房一處隱蔽的房頂上張望留晚。 院中可真熱鬧酵紫,春花似錦告嘲、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,871評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至参歹,卻和暖如春仰楚,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背犬庇。 一陣腳步聲響...
    開封第一講書人閱讀 32,994評論 1 269
  • 我被黑心中介騙來泰國打工僧界, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人臭挽。 一個月前我還...
    沈念sama閱讀 48,095評論 3 370
  • 正文 我出身青樓捂襟,卻偏偏與公主長得像,于是被迫代替她去往敵國和親欢峰。 傳聞我的和親對象是個殘疾皇子葬荷,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,884評論 2 354

推薦閱讀更多精彩內(nèi)容

  • 文章作者:Tyan博客:noahsnail.com 3.4 Dependencies A typical ente...
    SnailTyan閱讀 4,159評論 2 7
  • pyspark.sql模塊 模塊上下文 Spark SQL和DataFrames的重要類: pyspark.sql...
    mpro閱讀 9,453評論 0 13
  • Lua 5.1 參考手冊 by Roberto Ierusalimschy, Luiz Henrique de F...
    蘇黎九歌閱讀 13,797評論 0 38
  • 東北的守歲如同他們的人一樣干脆,豪爽纽帖,那里的熱鬧一點兒也不拖泥帶水宠漩。除夕夜里,他們徹夜不眠懊直,用自己獨特的方式慶祝著...
    深信文秘程詩11閱讀 330評論 0 1
  • 今天下午放元旦假 起床:7 任務(wù)清單 習(xí)慣養(yǎng)成:堅持日更 周目標(biāo)·完成進度 學(xué)習(xí)·信息·閱讀 健康·飲食·鍛煉 堅...
    了凡耕讀閱讀 155評論 0 1