[英]How to add points on SpatialLinesDataFrame using ggplot2?
[英]How to add location points onto a shapefile imported into R from QGIS using ggplot2
我正在從事一個涉及離岸位置的 GPS 坐標的項目。 我正在尋找測量每個點與海岸的距離。 我已經在 QGIS 中創建了一個相關海岸線的 shapefile,並且我已經使用st_read()
function (在本例中名為“biminishore”)成功地將其導入到 R 中。 使用以下代碼,我可以在 ggplot2 中 plot 我的 shapefile:
bplot = ggplot() +
geom_sf(data = biminishore, size = 0.1, color = "black", fill = "green1") +
ggtitle("Bimini, The Bahamas") +
coord_sf() +
theme_classic()
plot(bplot)
現在,我想添加位置坐標(作為 a.csv 導入到 R 中,Lat 和 Lon 具有單獨的列)作為導入 shapefile 上的層。 誰能建議如何 go 以一種允許我計算每個點與最近海岸線點之間的距離的方式執行此操作? 我目前的嘗試給出了錯誤: Error in st_transform.sfc(st_geometry(x), crs, ...): cannot transform sfc object with missing crs
我認為這意味着我的坐標系不兼容,但還沒有找到解決方法。 到目前為止,我已經嘗試使用SpatialPoints()
組合我的點列。 我也嘗試過使用st_set_crs()
和st_transform()
的多個 forms 但我還沒有運氣。 任何幫助是極大的贊賞! 謝謝!
將您的積分文件讀取為 csv,然后將其轉換為sf
object:
library(tidyverse)
library(sf)
points <- read_csv('path_to_points.csv')
#make it an sf object, change Long and Lat to the correct column name
points_sf <- st_as_sf(points, coords = c("Long", "Lat"))
# set crs of points_sf to same as biminishore object
points_sf <- st_set_crs(points_sf, st_crs(biminishore))
然后您應該能夠通過添加以下內容將它們一起 plot :
+ geom_sf(data = points_sf)
給您的 ggplot2 電話。
可以使用sf::st_nearest_feature(points_sf, biminishore)
找到兩者之間最近的特征。
關於最近特征和距離的好帖子: https://gis.stackexchange.com/questions/349955/getting-a-new-column-with-distance-to-the-nearest-feature-in-r
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.