r - biomod2 没有绘制数据
问题描述
我正在尝试运行一个基本的物种分布模型,而biomod2
不是绘制我的存在数据。我可以尝试在这里重新创建问题,但显然我无法上传我正在使用的 tif 文件。为了测试它,我简单地创建了一个经度/纬度值矩阵,对应于哥伦比亚的一条垂直线,如下所示:
tmp <- seq(-1,10,0.01) # latitude
tmp2 <- rep(-75,length(tmp)) # longitude
testVec <- cbind(tmp2,tmp)
myRespXY <- testVec
这些是我的存在数据的坐标。然后我只使用存在数据,所以我有
myResp <- as.numeric(rep(1,nrow(myRespXY)))
然后,使用格式化函数biomod2
,
myBiomodData <- BIOMOD_FormatingData(resp.var = myResp, # presence data (all 1's)
expl.var = myExpl, # RasterStack
resp.xy = myRespXY, # coordinates of presences, corresponding to a vertical line in Colombia
resp.name = myRespName) # name of species
plot(myBiomodData)
我究竟做错了什么?biomod2
正在阅读存在,有时它会绘制几个点,但它不会绘制大部分。如何获得输出以绘制所有存在数据?
解决方案
好吧,这不是软件包的错误,而是当您使用高分辨率环境变量栅格时出现的图形问题。
默认情况下,绘图函数根据那里的类(存在/不存在/未定义)着色像素。因为这里的像素太少了(与未定义区域相比,存在/不存在的地方太少了)我们无法在图中真正看到不同的颜色。
一种解决方法是从包的BIOMOD_FormatingData()
输出使用points
函数中提取存在/不存在,raster
以将它们显示在地图顶部。
这是一个简单的可重现示例:
##' This script is designed to show how you can simply extract presences, absences and pseudo absences
##' from a `data.formatted.Biomod.object` object (output of `BIOMOD_FormatingData()`).
##'
##' This can be particularly useful to produce custom plots when high resolution rasters are used within `biomod2` framework.
##'
##'
##' ### Data formatting
##'
##' Here we sill create a toy `data.formatted.Biomod.object` object using `BIOMOD_FormatingData()`
##' function.
##'
##' Here are couple parameters used for the data formatting procedure:
##'
##' - *species* : Gulo Gulo
##' - *explanatory variables*: [worlclim bioclimatic variables](http://www.worldclim.org/bioclim)
##' (bio_3, bio_4, bio_7, bio_11 & bio_12)
##' - *numbeer of absences*: 0
##' - *number of pseudo absences*: 500
##' - *number of pseudo absences sampling repetiton*: 2
##' - *pseudo absences selection algorithm*: `random`
##'
library(biomod2)
# species occurrences
DataSpecies <- read.csv(system.file("external/species/mammals_table.csv",
package="biomod2"), row.names = 1)
head(DataSpecies)
# the name of studied species
myRespName <- 'GuloGulo'
# the presence/absences data for our species
myRespDF <- DataSpecies[DataSpecies[, myRespName] == 1 ,c("X_WGS84","Y_WGS84", myRespName)]
myResp <- as.numeric(myRespDF[,myRespName])
# the XY coordinates of species data
myRespXY <- myRespDF[,c("X_WGS84","Y_WGS84")]
# Environmental variables extracted from Worldclim (bio_3, bio_4, bio_7, bio_11 & bio_12)
myExpl = raster::stack( system.file( "external/bioclim/current/bio3.grd",
package="biomod2"),
system.file( "external/bioclim/current/bio4.grd",
package="biomod2"),
system.file( "external/bioclim/current/bio7.grd",
package="biomod2"),
system.file( "external/bioclim/current/bio11.grd",
package="biomod2"),
system.file( "external/bioclim/current/bio12.grd",
package="biomod2"))
# Formatting data in biomod2 friendly sahpe
myBiomodData <- BIOMOD_FormatingData(resp.var = myResp,
expl.var = myExpl,
resp.xy = myRespXY,
resp.name = myRespName,
PA.nb.rep = 2,
PA.nb.absences = 500)
##'
##' ## The extraction funcrtions
##'
##' Here are define 2 small function that will help you to extract the points locations of a
##' `data.formatted.Biomod.object`
##'
##' **note**: This function will be integreted in a next release of `biomod2`
##'
library(dplyr)
## function to get PA dataset
get_PAtab <- function(bfd){
dplyr::bind_cols(
x = bfd@coord[, 1],
y = bfd@coord[, 2],
status = bfd@data.species,
bfd@PA
)
}
## function to get background mask
get_mask <- function(bfd){
bfd@data.mask
}
##'
##' ### Extract presences, absences and pseudo-absences tables
##'
##'
##' When you have extracted the PA table from `data.formatted.Biomod.object` you can easily select
##' presences (`filter(status == 1)`), pseudo-absences (`filter(is.na(status))`) or absences
##' (`filter(status == 0)`) even if no absences are dfined in our case
##'
## get the coordiantes of presences
(pres.xy <- get_PAtab(myBiomodData) %>%
filter(status == 1) %>%
select(x, y))
## get the coordiantes of absences ==> No absences here
(abs.xy <- get_PAtab(myBiomodData) %>%
filter(status == 0) %>%
select(x, y))
## get the coordiantes of pseudo - absences
## all repetition of pseudo absences sampling merged
(pa.all.xy <- get_PAtab(myBiomodData) %>%
filter(is.na(status)) %>%
select(x, y)) %>%
distinct()
## pseudo absences sampling for the first repetition only
(pa.1.xy <- get_PAtab(myBiomodData) %>%
filter(is.na(status) & PA1 == TRUE) %>%
select(x, y)) %>%
distinct()
##'
##' ### Plot example
##'
##'
##' Here is an example of plot that could be usefull when resolution is too high and presences
##' could not be diplayed correctly using the `data.formatted.Biomod.object` `plot()` function
##'
## plot the first PA selection and add the presences on top
plot(get_mask(myBiomodData)[['PA1']])
points(pres.xy, pch = 11)
这应该给你一个像这样的情节:
推荐阅读
- sql - 省去我写查询的时间
- loadrunner - ALM-PC - 尝试添加 PC 主机 VM 时出现错误“更改机器标识失败”
- eclipse - 运行我的 servlet 页面时出现 HTTP 错误 404,意味着使用 JSP 和 servlet 从数据库下载 Long BLOB 文件
- javascript - 检查用户是否登录passport.js
- mongodb - MongoError:未在 SessionDB 上授权执行命令 { listIndexes:“会话”,光标:{} }
- angular - Angular 5生产构建抛出错误
- ruby-on-rails - 红宝石中的时间增量
- ios - 使用 AudioKit 定期播放纯音
- javascript - 反应 this.setState 不更新我的值,火力变化正在显示
- java - Java 8 Optional 内 Optional