本期推文我们介绍一个可以绘制颇具“艺术”风格地图的可视化包-cartography,主要涉及的内容如下:
- R-cartography 简介
- R-cartography 实例应用
R-cartography 简介
说到cartography包,用Python绘图的小伙伴可能会想到cartopy(Basemap的下一代地图可视化绘制包),下面就简单介绍下cartography。
- 官网介绍
cartography官网如下:http://riatelab.github.io/cartography/docs/articles/cartography.html (可点击cartography官网)官方介绍如下:cartography包的目的是获得具有经典制图或GIS软件构建的主题图的视觉质量的主题图。用户可能属于以下两类之一:使用R的制图师或愿意创建地图的使用者。制图使用sf或sp对象生成基本图形。由于程序包的大多数内部结构都依赖于sf功能,因此空间对象的首选格式是sf。(官方直译的哈)
通过介绍我们可以知道,cartography主要基于sf对象进行绘图,所以我们在绘制之前需将数据(地图数据或者点数据)转换成sf对象。
- 可视化专题图介绍
cartography包官网提供了多种优秀的地图可视化绘制专题,这类可视化作品和一般的地图作品有些不一样,透露出一种“艺术 ”气息。这里我们列举几个比较样例供大家参考,更多样例可参看官网哦!
- 样例1
library(sf)
library(cartography)
# 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)
# download osm tiles
mtq.osm <- getTiles(
x = mtq,
type = "OpenStreetMap",
zoom = 11,
crop = TRUE
)
# plot osm tiles
tilesLayer(x = mtq.osm)
# plot municipalities (only borders are plotted)
plot(st_geometry(mtq), col = NA, border = "grey", add=TRUE)
# plot population
propSymbolsLayer(
x = mtq,
var = "POP",
inches = 0.25,
col = "brown4",
legend.pos = "topright",
legend.title.txt = "Total population"
)
# layout
layoutLayer(title = "Population Distribution in Martinique",
sources = "Sources: Insee and IGN, 2018n© OpenStreetMap contributors.nTiles style under CC BY-SA, www.openstreetmap.org/copyright.",
author = paste0("cartography ", packageVersion("cartography")),
frame = FALSE, north = FALSE, tabtitle = TRUE)
# north arrow
north(pos = "topleft")
可视化结果如下:
地图散点图
- 样例2
library(sf)
library(cartography)
# 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)
# transform municipality multipolygons to (multi)linestrings
mtq_pencil <- getPencilLayer(
x = mtq,
size = 400,
lefthanded = F
)
# plot municipalities (only the backgroung color is plotted)
plot(st_geometry(mtq), col = "white", border = NA, bg = "lightblue1")
# plot administrative status
typoLayer(
x = mtq_pencil,
var="STATUS",
col = c("aquamarine4", "yellow3","wheat"),
lwd = .7,
legend.values.order = c("Prefecture",
"Sub-prefecture",
"Simple municipality"),
legend.pos = "topright",
legend.title.txt = "",
add = TRUE
)
# plot municipalities
plot(st_geometry(mtq), lwd = 0.5, border = "grey20", add = TRUE, lty = 3)
# labels for a few municipalities
labelLayer(x = mtq[mtq$STATUS != "Simple municipality",], txt = "LIBGEO",
cex = 0.9, halo = TRUE, r = 0.15)
# title, source, author
layoutLayer(title = "Administrative Status",
sources = "Sources: Insee and IGN, 2018",
author = paste0("cartography ", packageVersion("cartography")),
north = FALSE, tabtitle = TRUE, postitle = "right",
col = "white", coltitle = "black")
# north arrow
north(pos = "topleft")
可视化结果如下:
铅笔风格主题地图
R-cartography 实例应用
我们使用之前空间插值系列的数据进行不同主题地图的绘制,首先 ,我们将所使用数据转换成sf对象,代码如下:
代码语言:javascript复制library(sf)
library(cartography)
library(openxlsx) # 读取Excel数据
jiangsu_shp <- "江苏省.json"
jiangsu <- sf::read_sf(jiangsu_shp)
file <- "pmdata.xlsx"
scatter_df <- read.xlsx(file)
scatter_sf <- st_as_sf(scatter_df,coords = c("lon", "lat"),crs = 4326)
接下来,我们进行部分样例的可视化绘制:
- 演示-1
plot(sf::st_geometry(jiangsu),col="#f2efe9", border="#b38e43", bg = "#aad3df",lwd = 0.5)
# plot PM2.5
propSymbolsLayer(
x = scatter_sf,
var = "PM2.5",
#inches = 0.18,
col = "brown4",
legend.pos = "topright",
legend.title.txt = "PM2.5"
)
# layout
layoutLayer(title = "PM2.5 Values in NanJing",
author = paste0("cartography ", packageVersion("cartography"),"nVisualization by DataCharm"),
frame = FALSE, north = FALSE, tabtitle = TRUE)
# north arrow
north(pos = "topleft")
可视化结果如下:
当然,我们还可以添加类别(label)属性进行绘制:
- 演示-2
#par(mar = c(0.5,1,0.5,0.5))
# Plot the municipalities
pdf("G:\DataCharm\可视化包介绍(绘制)\空间相关\cartography_02.pdf")
plot(st_geometry(jiangsu), col="#f2efe9", border="#b38e43", bg = "#aad3df",
lwd = 0.5)
# Plot symbols with choropleth coloration
propSymbolsTypoLayer(
x = scatter_sf,
var = "PM2.5",
inches = 0.25,
symbols = "square",
border = "white",
lwd = .5,
legend.var.pos = "topright",
legend.var.title.txt = "PM2.5",
var2 = "label",
legend.var2.values.order = c("1", "2","3","4"),
col = carto.pal(pal1 = "multi.pal", n1 = 4),
legend.var2.pos = c(117, 32.5),
legend.var2.title.txt = "Scatter Class"
)
# layout
layoutLayer(title="PM2.5 Values in NanJing",
author = paste0("cartography ", packageVersion("cartography"),"nVisualization by DataCharm"),
scale = 5, frame = FALSE, north = FALSE, tabtitle = TRUE)
# north arrow
north(pos = "topleft")
dev.off()
可视化结果如下:
资料获取
cartography 包还提供查找表(CHEAT SHEET),大家可以从上方的官网进行获取,当然,如果由于网速问题下载不到,也可以在本号(DataCharm)回复 cartography (建议直接)复制即可获取下载链接哦!