2

我正在尝试使用 R 中的 Cartogram 包创建全局制图。我正在尝试使用来自 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,但出现以下错误:“错误:使用未投影的地图。此函数没有为经度/纬度数据提供正确的质心和距离:使用“st_transform()”将坐标转换为另一个投影。 "

4

1 回答 1

2

取自此文档,据说

sf 对象的默认图是所有属性的多图,直到合理的最大值

如果要使用基本 Rplot函数,请使用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特别适合与ggplot2tidyverse。在该设置中,只需ggplot与 结合使用geom_sf

ggplot(world_cartogram) +
  geom_sf()
于 2022-02-02T08:50:58.330 回答