我正在嘗試使用凸包繪制物種范圍區域,然后計算該區域并創建一個圖形。
180 度國際日期變更線有一個眾所周知的問題,我一直在嘗試通過許多關于 SE 的例子來解決這個問題,例如:
如何使用 R 修復跨越國際日期變更線的路徑
這與我的目標很接近,但在 mapview 中繪圖而不是 ggplot2: 如何使用 sf 按因子從點構造/繪制多邊形的凸包?
這是我的嘗試:
library(tidyverse)
library(maps)
library(ggmap)
library(sf)
library(sp)
library(rnaturalearth)
library(rnaturalearthdata)
library(ggspatial)
library(mapproj)
生成物種出現資料,其中一些點跨越 180 經度
df <- data.frame(species = rep("sp1",8),
longitude = as.double(c(-170.2, -179.5, 55.9, 167.6, 154.3, 101.7, 70.54, -165.94)),
latitude = as.double(c(8.25, -24.75, 24.25,19.25, 33.45, -15.5, 5.56, 4.6)))
來自 map_data 和 plot 的以太平洋為中心的世界地圖
world <- map_data("world2")
map<-ggplot()
geom_polygon(data = world, aes(x = long, y = lat, group = group),
col = "#78909C", fill = "#78909C", lwd = 0)
coord_map(orientation = c(90,0, 150), ylim = c(-40, 40), xlim = c(20,210))
我的地圖
將出現點添加到地圖
map
geom_point(data = df, mapping = aes(x = longitude, y = latitude))
帶點的地圖
從物種發生資料構建最小凸包。
species.sf <- df %>%
st_as_sf( coords = c( "longitude", "latitude" ))
創建船體并環繞日期變更線
hull<- species.sf %>%
summarise( geometry = st_combine( geometry ) ) %>%
st_convex_hull()
hull<-st_wrap_dateline(hull,options = c("WRAPDATELINE=YES", "DATELINEOFFSET=180"),
quiet = TRUE)
Plot hull - 在 180 處切割,但顯然不包括所有出現點
map
geom_point(data = df, mapping = aes(x = longitude, y = latitude))
geom_sf(data=hull, inherit.aes = TRUE)
不正確的船體
計算船體面積 - 根據船體形狀必須不正確
st_area(hull)
我還嘗試將太平洋中心 CRS 應用于地圖、點和船體,但懷疑我以錯誤的順序或錯誤的位置應用這些?我對使用 R 進行空間分析非常陌生,因此任何幫助都非常有用。謝謝。
uj5u.com熱心網友回復:
請在下面找到解決您問題的方法。我使用st_shift_longitude()了包中的函式sf。
正品
- 您的資料(無變化)
df <- data.frame(species = rep("sp1",8),
longitude = as.double(c(-170.2, -179.5, 55.9, 167.6, 154.3, 101.7, 70.54, -165.94)),
latitude = as.double(c(8.25, -24.75, 24.25,19.25, 33.45, -15.5, 5.56, 4.6)))
world <- map_data("world2")
map<-ggplot()
geom_polygon(data = world, aes(x = long, y = lat, group = group),
col = "#78909C", fill = "#78909C", lwd = 0)
coord_map(orientation = c(90,0, 150), ylim = c(-40, 40), xlim = c(20,210))
map

- 將您的
dataframe“df”轉換為sf物件“species.sf”并使用st_shift_longitude()
species.sf <- df %>%
st_as_sf(coords = c("longitude", "latitude"), crs = 4326) %>%
st_shift_longitude()
map
geom_sf(data = species.sf, inherit.aes = TRUE)
coord_sf(xlim = c(40, 210), ylim = c(-40, 40))

- 計算基于
sf物件“species.sf”的凸包多邊形group_by(species)(對于您的一般情況)
hull <- species.sf %>%
group_by(species) %>%
summarise( geometry = st_combine( geometry ) ) %>%
st_convex_hull()
- 將
sf物件“hull”轉換回dataframe物件“hullDF”
hullDF <- hull %>%
st_geometry() %>%
st_coordinates() %>%
as.data.frame()
- 最終結果的可視化
map
geom_point(data = df, mapping = aes(x = longitude, y = latitude))
geom_polygon(data = hullDF, mapping = aes(x = X, y = Y), fill = "lightgreen", alpha = 0.5)

- 計算船體多邊形的面積(需要
units庫將結果轉換為平方千米)
library(units)
hull_area <- hull %>%
st_area() %>%
set_units(km^2)
hull_area
#> 74714882 [km^2]
由reprex 包(v2.0.1)于 2021 年 11 月 12 日創建
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/358904.html
