如何解决形状文件和位置坐标彼此不重叠
我正在尝试用特定位置的指针覆盖国家地图。为此,我首先下载了国家/地区的边界,然后下载了我想要绘制的点的纬度/经度值
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))
解决方法
您的 points
是 data.frame
而不是 SpatialPointsDataFrame
。因此,首先我使用 data.frame
将 SpatialPointsDataFrame
转换为 coordinates(points) <- ~longitude + latitude
。然后我给它分配了一个 crs
(+proj=longlat +datum=WGS84 +no_defs
)。然后我使用 sp
将 sf
对象转换为 st_as_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)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。