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

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

  • R-cartography 简介

  • R-cartography 实例应用

  • 所有完整代码都已整理之我们的线上课程,有需要的同学+v yidianshuyulove 咨询

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()

可视化结果如下:

优质学习资源推荐

相关推荐
青 春 记 忆3 小时前
Dify Docker Compose 通用无损升级指南:从备份、双版本预演到切换与回滚
运维·人工智能·python·docker·容器
GlueNa2SiO33 小时前
03-Flask模板引擎Jinja2详解
笔记·python·flask
临沂GEO6 小时前
GEO搜索优化科普|正规地理位置流量运营入门指南
大数据·人工智能·python·流量运营
大模型码小白6 小时前
Spring AI Tool 实现自然语言操作 MySQL 数据库详解
服务器·开发语言·数据库·人工智能·python·mysql·spring
RE-19016 小时前
电商互联网指标 - 归纳笔记
数据分析·电商指标·互联网指标·业务指标·指标解释
荷蒲7 小时前
【小白量化Qbuddy】利用AI学习中文Python
人工智能·python·学习
柳絮飞祭奠8 小时前
Dify Windows Docker Desktop 部署文档
人工智能·深度学习·语言模型·数据分析·transformer·边缘计算·集成学习
数据狐(Datafox)9 小时前
京东商品列表API技术解析与落地应用(含标准 JSON 示例)
java·大数据·前端·人工智能·python·数据分析·json
量化吞吐机9 小时前
2026年下半年手工交易规则走向量化表达,产品该先接住哪一步
人工智能·python
Metaphor6929 小时前
使用 Python 读取和删除 Excel 文件属性
python·excel