使用 R 的地图:无法更改 points/coordinates 的投影
Maps with R: Can't change the projection for points/coordinates
我想绘制一个包含多个点的世界地图,也就是经纬度坐标的组合。
我不想使用墨卡托,因此我重新投影了世界地图的数据和我的坐标。
当世界的投影发生变化时,所有点突然位于地图的中间(一种常见行为,当投影未对齐时,请参阅 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")
结果:
但是,当我使用标准投影 WGS84 时,它确实有效,即 crs = 4326
):
您的 points
数据帧的坐标是根据 lat/lon 定义的,与 EPSG 4326 一致。您应该将其转换为具有特定 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)
你的代码应该可以工作。
我想绘制一个包含多个点的世界地图,也就是经纬度坐标的组合。
我不想使用墨卡托,因此我重新投影了世界地图的数据和我的坐标。
当世界的投影发生变化时,所有点突然位于地图的中间(一种常见行为,当投影未对齐时,请参阅 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")
结果:
但是,当我使用标准投影 WGS84 时,它确实有效,即 crs = 4326
):
您的 points
数据帧的坐标是根据 lat/lon 定义的,与 EPSG 4326 一致。您应该将其转换为具有特定 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)
你的代码应该可以工作。