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

可视化结果如下:

优质学习资源推荐

相关推荐
databook8 分钟前
manim边做边学--圆柱体
python·动效
deephub37 分钟前
Scikit-learn Pipeline完全指南:高效构建机器学习工作流
人工智能·python·机器学习·scikit-learn
麻衣带我去上学1 小时前
Pytest使用Jpype调用jar包报错:Windows fatal exception: access violation
windows·python·pytest·jar
李昊哲小课1 小时前
springboot整合hive
大数据·数据仓库·hive·spring boot·后端·数据分析
易风有点疯1 小时前
Python:序列化
开发语言·python
亚图跨际1 小时前
Python和R统计检验比较各组之间的免疫浸润
python·r语言·统计检验
WangYan20221 小时前
结构方程模型(SEM)入门到精通:lavaan VS piecewiseSEM、全局估计/局域估计;潜变量分析、复合变量分析、贝叶斯SEM在生态学领域应用
r语言·生态学·结构方程模型·sem·lavaan·piecewisesem
Biomamba生信基地1 小时前
R语言基础| 时间序列分析
开发语言·r语言·kotlin·编程
Promising_GEO1 小时前
使用R语言绘制简单地图的教程
开发语言·python·r语言
湫ccc2 小时前
《Python基础》之函数的用法
开发语言·python