2

我正在尝试使用 maptools 包中的 wrld_simple 数据根据 2005 年的人口绘制涉及一些欧洲国家的地图:

countries = c('Austria', 'Belgium', 'Bulgaria', 'Cyprus', 'Czech Republic', 'Denmark', 'Estonia', 'Finland', 'France', 'Germany', 'Greece', 'Hungary', 'Iceland', 'Ireland', 'Italy', 'Latvia', 'Lithuania', 'Luxembourg', 'Malta', 'Netherlands', 'Norway', 'Poland', 'Portugal', 'Slovenia', 'Spain', 'Sweden', 'United Kingdom')

使用 maptools 包,我得到了 wrld_simpl 数据,并检索了这些特定国家的数据:

data(wrld_simpl)

mapsimple = wrld_simpl[wrld_simpl$NAME %in% countries]

plot(mapsimple)

到目前为止一切顺利,但是当我将其绘制出来时:

cartogram= cartogram(mapping1, "POP2005", itermax = 7)

Please use cartogram_cont() instead of cartogram().

Error: Using an unprojected map. This function does not give correct centroids and distances for longitude/latitude data:
Use "st_transform()" to transform coordinates to another projection.

我尝试使用 Cartogram_cont:

> cartogram= cartogram_cont(mapping1, "POP2005", itermax = 7)

Error: Using an unprojected map. This function does not give correct centroids and distances for longitude/latitude data:
Use "st_transform()" to transform coordinates to another projection.

我尝试将其转换为 sf,然后执行 st_transform(),但无济于事:

sfno = st_as_sf(mapping1)

sfnoproj = st_transform(sfno, coords = c("lon", "lat"), crs = "+proj=longlat +datum=WGS84", agr = "constant")

cartogram= cartogram_cont(sfnoproj, "POP2005", itermax = 7)

Error: Using an unprojected map. This function does not give correct centroids and distances for longitude/latitude data:

Use "st_transform()" to transform coordinates to another projection.

我假设我没有正确的投影值。我怎么知道设置得更好?如果我要使用“POP2005”以外的其他值,我该怎么做?图表中的“itermax”是什么意思(我不太明白给定的定义)

任何帮助将不胜感激,因为我对此很陌生。谢谢!!!!

4

1 回答 1

2

您的示例数据中存在一些不一致(mapsimplev.mapping1和缺少逗号),因此更新如下:

library(maptools)
library(sf)
library(ggplot2)

countries = c('Austria', 'Belgium', 'Bulgaria', 'Cyprus', 'Czech Republic', 'Denmark', 'Estonia', 'Finland', 'France', 'Germany', 'Greece', 'Hungary', 'Iceland', 'Ireland', 'Italy', 'Latvia', 'Lithuania', 'Luxembourg', 'Malta', 'Netherlands', 'Norway', 'Poland', 'Portugal', 'Slovenia', 'Spain', 'Sweden', 'United Kingdom')

data(wrld_simpl)

mapsimple = wrld_simpl[wrld_simpl$NAME %in% countries,]

如错误所示,您的数据未投影。它使用纬度/经度,这不适用于cartogram_cont().

sfno <- st_as_sf(mapsimple)
st_crs(sfno)

Coordinate Reference System:
  EPSG: 4326 
  proj4string: "+proj=longlat +datum=WGS84 +no_defs"

st_transform()正在重新投影sfno为同一件事:EPSG 4326:

sfnoproj <- st_transform(sfno, coords = c("lon", "lat"), 
                        crs = "+proj=longlat +datum=WGS84", 
                        agr = "constant")
st_crs(sfnoproj)

Coordinate Reference System:
  EPSG: 4326 
  proj4string: "+proj=longlat +datum=WGS84 +no_defs"

相反,请为欧洲选择投影坐标系,例如23038

sfproj <- st_transform(sfno, crs = 23038)
st_crs(sfproj)

Coordinate Reference System:
  EPSG: 23038 
  proj4string: "+proj=utm +zone=38 +ellps=intl +towgs84=-87,-98,-121,0,0,0,0 +units=m +no_defs"

然后,我们可以制作并绘制图表:

cartogram <- cartogram_cont(sfproj, "POP2005", itermax = 7)

ggplot() +
  geom_sf(data = cartogram, aes(fill = POP2005))

在此处输入图像描述

于 2020-01-19T16:46:59.150 回答