我在 csv 中有一系列来自墨西哥的纬度和经度点,我将其转换为
sf
对象。我能够使用 http://projfinder.com/. 识别 crs
library(sf)
library(spData)
library(tibble)
# basemap
mx = world %>% filter(iso_a2 == 'MX')
# cast to WGS84
mx = st_transform(mx, crs='EPSG:4326')
# mypoints
mypoints = tibble(
latitude = c(19.46762, 32.63224, 18.94691, 19.28556, 18.92243),
longitude = c(-98.14863, -115.5587, -103.9721, -99.13365, -99.22217)
)
mypoints_geo = st_as_sf(mypoints, coords = c("longitude", "latitude"), crs = 'EPSG:4326')
# plot
plot(mx['iso_a2'], axes=T)
plot(mypoints, pch = 3, col = 'red', add=T)
正如您在第一张图片中看到的,这些点并不位于墨西哥;事实上,它们甚至似乎不位于纬度/经度值所在的位置。我附上了我在
geopandas
中所做的替代实现的另一张图像,效果很好。我需要如何修改 R 实现才能获得所需的结果?
我已经尝试过:
ggplot2
实现绘图。预期结果来自上面的 geopandas 实现图像。
我设法在 ggplot 上重现了这个,这是我使用的代码:
# Convert mx to WGS84
mx <- st_transform(world %>% filter(iso_a2 == 'MX'),
crs = 'EPSG:4326')
# mypoints
mypoints = tibble(
latitude = c(19.46762, 32.63224, 18.94691, 19.28556, 18.92243),
longitude = c(-98.14863, -115.5587, -103.9721, -99.13365, -99.22217)
)
mypoints_geo = st_as_sf(mypoints, coords = c("longitude", "latitude"), crs = 'EPSG:4326')
# Plot using ggplot2
ggplot() +
geom_sf(data = mx, fill = "cornflowerblue", color = "black") +
geom_sf(data = mypoints_geo, color = "red",fill = "red", size = 4, shape = 21) +
theme_minimal()