r - 带有 R : Can't change the projection for points/coordinates 的 map

标签 r ggplot2 spatial r-sf

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

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

当世界的投影发生变化时,所有点都会突然放置在 map 的中间(这是一种常见行为,当投影不对齐时,请参阅 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

最佳答案

您的数据框的坐标是根据纬度/经度定义的,与 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

关于r - 带有 R : Can't change the projection for points/coordinates 的 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55221669/

相关文章:

c++ - OpenCV 中空间域的 DFT 不起作用

mysql - 如何在 MySql 中将字符串 "lat, lon"转换为 POINT

r - 获取 S4 对象的插槽值?

r - 如何在 R 中按州创建分层样本

r - ggplot2 条形图,条内有线条

旋转箱线图图例(R,ggplot2)

r - 使用 ggplot2 在 Venn 中自定义重叠区域的颜色

r - 检查模型是否只有一个因子协变量

javascript - 具有北极投影的传单和 R

如果 purrr :possibly() fails 内的表达式返回映射对象