有条件地将XY点添加到由levelplot生成的栅格地图

时间:2015-03-21 02:22:20

标签: r maps levelplot

我这里有一个棘手的ifelse任务。以下是我的代码,显示了两个时段(futurecurrent)的数据。数据与xy坐标一起有mean, 5th and 95th置信区间。我想比较两个dfs(mean, 5th and 95thfuture)的current置信区间(CI)。

条件:

1)如果CIs的{​​{1}}与future的{​​{1}}重叠,且futrue的CI高于当前值,则为current

2)如果pch=2的{​​{1}}与CIs的{​​{1}}不重叠,且futrue的CI低于当前值,则为future

3)如果未来的CI与当前的CI重叠,则为current

pch=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)

例如,在下图中,如果NFC(conditions)的最小值完全高于AFC的最大值(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) ),那么条件1.如果NFC的最大值完全低于AFC的最小值,然后是条件1.如下图所示满足条件3。

enter image description here

请帮忙。 AT

1 个答案:

答案 0 :(得分:1)

如果定义完整的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')