地图可视化绘制 | 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()

可视化结果如下:

优质学习资源推荐

相关推荐
西柚与蓝莓1 小时前
【开源开放体系总结】
python
belldeep5 小时前
python:reportlab 将多个图片合并成一个PDF文件
python·pdf·reportlab
FreakStudio7 小时前
全网最适合入门的面向对象编程教程:56 Python字符串与序列化-正则表达式和re模块应用
python·单片机·嵌入式·面向对象·电子diy
丶21367 小时前
【CUDA】【PyTorch】安装 PyTorch 与 CUDA 11.7 的详细步骤
人工智能·pytorch·python
_.Switch8 小时前
Python Web 应用中的 API 网关集成与优化
开发语言·前端·后端·python·架构·log4j
一个闪现必杀技8 小时前
Python入门--函数
开发语言·python·青少年编程·pycharm
小鹿( ﹡ˆoˆ﹡ )9 小时前
探索IP协议的神秘面纱:Python中的网络通信
python·tcp/ip·php
卷心菜小温9 小时前
【BUG】P-tuningv2微调ChatGLM2-6B时所踩的坑
python·深度学习·语言模型·nlp·bug
陈苏同学9 小时前
4. 将pycharm本地项目同步到(Linux)服务器上——深度学习·科研实践·从0到1
linux·服务器·ide·人工智能·python·深度学习·pycharm
唐家小妹9 小时前
介绍一款开源的 Modern GUI PySide6 / PyQt6的使用
python·pyqt