将数据点坐标到 R 中的 shapefile 上

问题描述 投票:0回答:1

我在 CRS 中拥有坐标数据(新西兰)和 shapefile - WGS 84 / Mercator 41,“EPSG”,3994。我想将坐标数据点放置在形状文件上。我可以绘制形状文件:

但我无法在上面放置点。我尝试过此代码的各种版本:

eff_sp4 <- st_transform(eff_sp4, st_crs(CRA6_shp))

ggplot(data = CRA6_shp) +
geom_sf() + 
geom_sf(data = eff_sp4, color = "steelblue", size = 2) +
scale_fill_discrete("Statistical Area")+
labs(title = "Potlifts in CRA6 during Fishing Years 2014 - 2023",
   x = "Longitude", y = "Latitude")+
theme(plot.title = element_text(hjust = 0.5), 
    axis.title.x = element_text(margin = margin(t = 10)),  
    axis.title.y = element_text(margin = margin(r = 10)))  

这只会产生以下结果:

ggplot2 spatial shapefile ggmap
1个回答
0
投票

我认为关键点是你应该获取要添加到地图中的点的坐标。使用

st_coordinates()
提取坐标或其他方法来获取数据框中的纬度和经度。使用
geom_sf()
绘制地图并使用
geom_point()
添加点。

library(ggplot2)
library(sf)

# map data
nc <- sf::st_read(system.file("shape/nc.shp", package="sf"))


# replace 'nc' with your coordinate data here, may be 'eff_sp4'
# for convenience, just select 5 points here
data_points <- st_coordinates(nc)[1:5, ]
data_points
#             X        Y L1 L2 L3
#[1,] -81.47276 36.23436  1  1  1
#[2,] -81.54084 36.27251  1  1  1
#[3,] -81.56198 36.27359  1  1  1
#[4,] -81.63306 36.34069  1  1  1
#[5,] -81.74107 36.39178  1  1  1


ggplot() +
  geom_sf(data = nc) +
  geom_point(data = data_points, aes(x = X, y = Y))
© www.soinside.com 2019 - 2024. All rights reserved.