带R的地图:无法更改点/坐标的投影 [英] Maps with R: Can't change the projection for points/coordinates
问题描述
我想绘制包含多个点(又称为纬度和经度坐标的组合)的世界地图.
我不想使用Mercator,因此我重新投影了世界地图的数据和我的坐标.
当世界的投影发生变化时,所有点都突然放置在地图的中间(一种常见的行为,当投影未对齐时,请参见
确实可以,但是,当我使用标准投影WGS84时,即 crs = 4326
):
点
数据框的坐标以经/纬度定义,与EPSG 4326一致.您应该对其进行转换转换为具有特定 crs
参数的 sf
对象,然后再将其转换为其他坐标系.
替换此:
点<-st_as_sf(点,坐标= c(经度",纬度"),crs = crs)
与此:
点<-st_as_sf(点,坐标= c(经度",纬度"),crs = 4326)点<-st_transform(点,crs = crs)
您的代码应该可以工作.
I want to plot a world map with multiple points aka combinations of latitude and longitude coordinates.
I don't want to use Mercator, therefore I re-project both, the data for the world map and my coordinates.
While the projection for the world changes, all points are suddenly placed in the middle of the map (a common behavior, when the projections don't align, see https://www.earthdatascience.org/courses/earth-analytics/spatial-data-r/intro-to-coordinate-reference-systems/).
What am I doing wrong in when assigning the projection to the points?
My code:
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")
The result:
It does work, however, when I use the standard projection WGS84, i.e. crs = 4326
):
The coordinates of your points
dataframe were defined in terms of lat/lon, which are congruent with EPSG 4326. You should convert it into an sf
object with that specific crs
parameter, before transforming it to other coordinate systems.
Replace this:
points <- st_as_sf(points, coords = c("longitude", "latitude"), crs = crs)
With this:
points <- st_as_sf(points, coords = c("longitude", "latitude"), crs = 4326)
points <- st_transform(points, crs = crs)
And your code should work.
这篇关于带R的地图:无法更改点/坐标的投影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!