[英]Add XY points conditionally to raster map generated by levelplot
我这里有一个棘手的ifelse
任务。 以下是我的代码,显示了两个时期( future
和current
)的数据。 数据在xy坐标旁边具有mean, 5th and 95th
置信区间。 我想比较两个df( future
和current
)的mean, 5th and 95th
置信区间(CI)。
条件:
1)如果future
的CIs
与current
配置项不重叠,并且future
配置项高于当前配置项,则pch=2
。
2)如果future
的CIs
与current
配置项不重叠,并且future
配置项低于当前配置项,则pch=3
。
3)如果将来的配置项与当前的配置项重叠,则pch=4
library(raster)
library(rasterVis)
s <- stack(replicate(2, raster(matrix(runif(100), 3))))
current <- data.frame(coordinates(sampleRandom(s, 3, sp=TRUE)),
C5th=c(17.643981,16.83572,9.979904),
CMean=c(26.66364,19.74286,15.10000),C95th=c(35.68329,22.64999,20.22010))
future <- data.frame(coordinates(sampleRandom(s, 3, sp=TRUE)),
C5th=c(17.643981,16.83572,9.979904)*2,
CMean=c(26.66364,19.74286,15.10000)*2,C95th=c(35.68329,22.64999,20.22010)*2)
以上三个conditions
的结果将被添加到我的地图中。 有点像(只是尝试):
levelplot(s, margin=FALSE, at=seq(0, 1, 0.05)) +
layer(sp.points(xy, pch=ifelse(condition, 2, 3,4), cex=2, col=1), columns=1) +
layer(sp.points(xy, pch=ifelse(condition, 2, 3,4), cex=2, col=1), columns=2)
例如,在下图中,如果NFC的最小值( future
)完全高于AFC的最大值( current
),则条件1.如果NFC的最大值完全低于AFC的最小值,则条件1。如下所示满足条件3。
请帮忙。 在。
如果您定义一个完整的SpatialPointsDataFrame
对象,并根据需要的条件定义一个附加的分类变量, SpatialPointsDataFrame
容易。
library(raster)
library(rasterVis)
s <- stack(replicate(2, raster(matrix(runif(1000), 3))))
## Coordinates
cc <- sampleRandom(s, 3, sp = TRUE)
## Data
current <- data.frame(C5th=c(17.643981,16.83572,9.979904),
CMean=c(26.66364,19.74286,15.10000),
C95th=c(35.68329,22.64999,20.22010))
future <- data.frame(C5th=c(17.643981,16.83572,9.979904)*2,
CMean=c(26.66364,19.74286,15.10000)*2,
C95th=c(35.68329,22.64999,20.22010)*2)
cf <- data.frame(current, future)
## Define a categorical variable checking the conditions you need
cf$class <- with(cf,
ifelse(C5th > C95th.1, 'A',
ifelse(C95th < C5th.1, 'B',
ifelse(C5th < C95th.1 && C5th > C5th.1, 'C', 'D')
)
)
)
cf$class <- factor(cf$class)
## Build a SPDF object with the coordinates and data
pp <- SpatialPointsDataFrame(cc, cf)
可以使用spplot
显示该对象。 有了它,您可以选择符号,大小等。
levelplot(s) + spplot(pp["class"],
pch = 21:23,
col.regions = 'gray')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.