R中的地图:无法更改点/坐标的投影

6

我想绘制一个世界地图,上面有多个点,即纬度和经度坐标的组合。

我不想使用墨卡托投影,因此我重新投影了世界地图数据和我的坐标。

当地图投影改变时,所有点突然被放置在地图的中间(当投影不对齐时,这是一种常见行为,请参见https://www.earthdatascience.org/courses/earth-analytics/spatial-data-r/intro-to-coordinate-reference-systems/)。

在将投影分配给点时,我做错了什么?

我的代码:

library(ggplot2)
library(sf)
library(rnaturalearth)

# assign a projection, for example ... 
crs <- 3035

# get data for the world map and assign the projection
world <- ne_countries(scale = "medium", returnclass = "sf")
world <- st_transform(world, crs = crs)

# create data frame with three points, convert it to a spatial object 
# and assign the same projection
points <- data.frame(longitude = c(-105.2519, 10.7500, 2.9833),
                     latitude = c(40.0274, 59.9500, 39.6167))

points <-  st_as_sf(points, coords = c("longitude", "latitude"), crs = crs)

# plot the data with ggplot2:
ggplot() + 
  geom_sf(data = world) +
  geom_sf(data = points, color = "red")

结果:

结果:

enter image description here

然而,当我使用标准的投影WGS84(即crs = 4326)时,它确实有效:

enter image description here

1个回答

9

您的points数据框中的坐标是使用与EPSG 4326相对应的纬度/经度定义的。在将其转换为其他坐标系统之前,您应该将其转换为具有该特定crs参数的sf对象。

请用以下内容替换:

points <- st_as_sf(points, coords = c("longitude", "latitude"), crs = crs)

使用此方法:

points <- st_as_sf(points, coords = c("longitude", "latitude"), crs = 4326)
points <- st_transform(points, crs = crs)

你的代码应该能够正常工作。

plot


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接