R语言 sf打印时点位置关闭

wf82jlnq  于 9个月前  发布在  其他
关注(0)|答案(1)|浏览(73)

我在一个csv中有一系列来自墨西哥的纬度和经度点,我正在将其转换为一个sf对象。

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_geo, pch = 3, col = 'red', add=T)

字符串
正如你在第一张图中看到的,这些点并不位于墨西哥;事实上,它们甚至看起来都不位于经纬度值所在的位置。我附加了另一张图,它来自我在geopandas中使用的另一个实现,它工作正常。我需要如何修改R实现才能得到想要的结果?

我试过:

  • 更改初始CRS并转换为WGS 84
  • 执行情况
  • 使用在线工具搜索点位置
  • 或者使用ggplot2进行绘图。

预期的结果是上面这张图的实现。

toiithl6

toiithl61#

我设法在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()

字符串

输出:


的数据

相关问题