首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将XY点有条件地添加到水准图生成的栅格图中

将XY点有条件地添加到水准图生成的栅格图中
EN

Stack Overflow用户
提问于 2015-03-21 02:22:19
回答 1查看 411关注 0票数 0

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

条件:

1)如果CIs for future不与current重叠,则pch=2

2)如果CIs for future不与currentcurrentpch=3重叠,则pch=3

3)如果未来的顺铂与当前的有重叠,那么pch=4

代码语言:javascript
复制
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的结果添加到我的地图中。类似于(仅仅是一次尝试):

代码语言:javascript
复制
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的最小值,那么condtion 1.如下所示的图满足条件3。

请帮帮忙。在…。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-22 15:05:15

如果您定义了一个完整的SpatialPointsDataFrame对象,并根据您需要的条件定义了一个额外的分类变量,那就更容易了。

代码语言:javascript
复制
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显示。有了它,你可以选择符号,大小等。

代码语言:javascript
复制
levelplot(s) + spplot(pp["class"],
                      pch = 21:23,
                      col.regions = 'gray')
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29178553

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档