我正在尝试使用 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)
你知道代码有什么问题吗?也许是 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))
现在,sf
特别适合 ggplot2
和 tidyverse。在该设置中,只需将 ggplot 与 geom_sf 结合使用即可。
ggplot(world_cartogram) +
geom_sf()
关于r - R 中的全局 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70951039/