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

时间:2019-03-18 12:42:41

标签: r ggplot2 spatial sf

我想绘制包含多个点(又称为纬度和经度坐标组合)的世界地图。

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

当世界的投影发生变化时,所有点都突然放置在地图的中间(一种常见的行为,当投影未对齐时,请参见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 个答案:

答案 0 :(得分:1)

points数据帧的坐标是根据经纬度定义的,与EPSG 4326一致。您应该将其转换为具有特定sf参数的crs对象,然后再将其转换为其他坐标系。

替换此:

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