我正在研究一些带有 GPS 项圈的动物,并使用 adehabitatHR 包中的 LoCoH.a 函数来生成 SpatialPolygonsDataFrame。
library(tidyverse)
library(lubridate)
library(adehabitatHR)
library(sp)
library(sf)
library(ggplot2)
library(maptools)
coordinates(birds1) <- c("x", "y")
birds.xy <- SpatialPoints(birds1)
birds.area <- LoCoH.a(birds.xy, a = 5000, unin = c("m"), unout = c("km2"), duplicates = "random")
class(birds.area)
ggplot(birds.area) + geom_sf()
一切正常,直到我尝试用 ggplot 绘制结果对象。我收到以下错误:
> ggplot(birds.area) + geom_sf()
Regions defined for each Polygons
Error in `geom_sf()`:
! Problem while computing stat.
ℹ Error occurred in the 1st layer.
Caused by error in `compute_layer()`:
! `stat_sf()` requires the following missing aesthetics: geometry
Run `rlang::last_trace()` to see where the error occurred.
我验证了“birds.area”的类是适当的格式,这是其他帖子中发现的问题,但是我无法弄清楚为什么 SpatialPolygonsDataFrame 中会缺少几何图形。
在使用
birds.area
和 SpatialPolygonsDataFrame
之前,您需要将 sf
从 ggplot
转换为 geom_sf
对象。您可以通过以下方式执行此操作:
library(sf)
birds.area <- st_as_sf(birds.area)