Shapefile 和位置坐标不相互重叠

Shapefile and location coordinates don't overlap each other

我正在尝试用指向特定位置的指针覆盖国家/地区地图。为此,我首先下载了国家边界,然后下载了我想要绘制的点的 lat/lon 值

library(rgeoboundaries)
boundary <- geoboundaries("Mongolia")

library(MODIStools)
points <- mt_sites() %>%
  filter(country == "Mongolia")

然后我尝试使用 ggplot2 将它们绘制在一起,但它们不会相互重叠。

library(ggplot2)
ggplot() +
  geom_sf(data = boundary) +
  geom_point(data = points,
             aes(x = latitude,
                 y = longitude))

你的 pointsdata.frame 而不是 SpatialPointsDataFrame。因此,首先我使用 coordinates(points) <- ~longitude + latitudedata.frame 转换为 SpatialPointsDataFrame。然后我给它分配了一个 crs (+proj=longlat +datum=WGS84 +no_defs)。然后我使用 st_as_sfsp 对象转换为 sf 对象,然后我将其绘制为

library(rgeoboundaries)
library(raster)
library(tidyverse)
library(sf)
library(MODISTools)

boundary <- geoboundaries("Mongolia")

points <- mt_sites() %>%
  filter(country == "Mongolia")

#Check the coordinate reference of points and boundary
crs(points)
#> [1] NA
crs(boundary)
#> CRS arguments: +proj=longlat +datum=WGS84 +no_defs 

#See the class of the points
class(points)
#> [1] "data.frame"

#Convert the data.frame to SpatialPointsDataFrame
coordinates(points) <- ~longitude + latitude

#Assign the crs to points SpatialPointsDataFrame
crs(points) <- crs(boundary)

#Convert sp object to sf object
points_sf <- st_as_sf(points)

#Plot the results
ggplot() +
  geom_sf(data = boundary) +
  geom_sf(data = points_sf)