r - R 中的全局 map

标签 r spatial r-sf cartogram

我正在尝试使用 R 中的 cartogram 包创建全局 map 。我正在尝试使用 wrld_simpl 中的数据。我期望的是一个绘制人口(“Pop2005”变量)的图表。我开发的代码是这样的:

data(wrld_simpl)
world<-wrld_simpl

world_sf = st_as_sf(world)
world_sf_proj = st_transform(world_sf, crs = 3785)

world_cartogram <- cartogram_cont(world_sf_proj, "POP2005")
plot(world_cartogram)

尽管如此,这还是导致了下图: enter image description here

你知道代码有什么问题吗?也许是 CRS?我尝试使用其他CRS,但出现以下错误: “错误:使用未投影的 map 。此函数无法给出经度/纬度数据的正确质心和距离: 使用“st_transform()”将坐标转换为另一个投影。”

最佳答案

取自 this documentation ,据说

The default plot of an sf object is a multi-plot of all attributes, up to a reasonable maximum

如果您想使用基本 R plot 函数,请使用 st_geometry(your_map) 绘制(几何图形)sf 对象.

另一种可能性(我不推荐)是将绘图选项设置为 1 个绘图最大值 (options(sf_max.plot=1)),但这会绘制第一个变量,并且可能这不是最好的主意。

library(sf)
library(spData)
library(cartogram)
library(tidyverse)

world_sf = st_as_sf(world)
world_sf_proj = st_transform(world_sf, crs = 3785)
world_cartogram <- cartogram_cont(world_sf_proj, "pop")

plot(st_geometry(world_cartogram))

enter image description here


现在,sf 特别适合 ggplot2 和 tidyverse。在该设置中,只需将 ggplot 与 geom_sf 结合使用即可。

ggplot(world_cartogram) +
  geom_sf()

关于r - R 中的全局 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70951039/

相关文章:

R - 组合 R 中数据帧内的重复行 :

r - 基于距 0 :0 lines 距离的颜色渐变散点图

r - 如何从R中的字符串列表中删除元素

Mysql st_intersects + st_buffer 不起作用

r - 使用 GEOJSON_WRITE 在 R 中创建 GEOJSON 文件

r - 取消嵌套包含 sf 对象的列表列

r - 将变量递增 N 次并将答案存储在矩阵中

linq-to-sql - Linq2Sql或EF4中的空间数据类型支持

postgresql - Postgres + postgis 中 x,y 坐标集合的凸包生成

r - 使用 {stars} 包对 Sentinel-2 数据进行远程 (vsicurl) 计算?