首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >地图可视化绘制 | R-cartography 艺术地图绘制

地图可视化绘制 | R-cartography 艺术地图绘制

作者头像
DataCharm
发布2021-02-22 11:49:21
8571
发布2021-02-22 11:49:21
举报

本期推文我们介绍一个可以绘制颇具“艺术”风格地图的可视化包-cartography,主要涉及的内容如下:

  • R-cartography 简介
  • R-cartography 实例应用

R-cartography 简介

说到cartography包,用Python绘图的小伙伴可能会想到cartopy(Basemap的下一代地图可视化绘制包),下面就简单介绍下cartography。

  1. 官网介绍

cartography官网如下:http://riatelab.github.io/cartography/docs/articles/cartography.html (可点击cartography官网)官方介绍如下:cartography包的目的是获得具有经典制图或GIS软件构建的主题图的视觉质量的主题图。用户可能属于以下两类之一:使用R的制图师或愿意创建地图的使用者。制图使用sf或sp对象生成基本图形。由于程序包的大多数内部结构都依赖于sf功能,因此空间对象的首选格式是sf。(官方直译的哈)

通过介绍我们可以知道,cartography主要基于sf对象进行绘图,所以我们在绘制之前需将数据(地图数据或者点数据)转换成sf对象。

  1. 可视化专题图介绍

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, 2018\n© 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对象,代码如下:

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 (建议直接)复制即可获取下载链接哦!

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-01-14,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 DataCharm 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • R-cartography 简介
  • R-cartography 实例应用
  • 资料获取
相关产品与服务
灰盒安全测试
腾讯知识图谱(Tencent Knowledge Graph,TKG)是一个集成图数据库、图计算引擎和图可视化分析的一站式平台。支持抽取和融合异构数据,支持千亿级节点关系的存储和计算,支持规则匹配、机器学习、图嵌入等图数据挖掘算法,拥有丰富的图数据渲染和展现的可视化方案。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档