在R中使用rasterVis在堆栈的每一层上绘制特定点数据 [英] Plot specific point data on each layer in stack with rasterVis in R
问题描述
我有2个栅格堆叠在一起:
I have 2 rasters stacked together:
library(rasterVis)
r1 <- raster(system.file("external/test.grd", package="raster"))
r2 <- r1 / 2
r.stack <- stack(r1, r2)
由于稍后我想突出显示堆栈中每个特定层的某些区域,因此我基于栅格值创建了两个点数据集:
Since I would like to highlight some areas later for each specific layer in the stack, I create two point datasets based on the raster values:
pts1 <- rasterToPoints(r1, spatial=T)
idx <- which(as.data.frame(pts1)[, 1] >= 400)
pts1 <- pts1[idx, 1]
pts2 <- rasterToPoints(r2, spatial=T)
idx <- which(as.data.frame(pts2)[, 1] >= 400)
pts2 <- pts2[idx, 1]
现在,我想用R中 rasterVis 中的levelplot绘制栅格堆栈.我也想将r1与pts1叠加,将r2与pts2叠加.
Now, I would like to plot the raster stack with levelplot from rasterVis in R. I would also like to overlay r1 with pts1 and r2 with pts2.
但是,一旦我添加了一个点数据集,就将其用于两个图层:
However, as soon as I add one point dataset, it is used for both layers:
levelplot(r.stack) + layer(sp.points(pts1, pch=20, cex=0.1, col="black"))
在仍然使用栅格堆栈的情况下,如何在特定图层上使用特定点数据集?
How can I use specific point datasets with specific layers while still using raster stacks?
我想避免制作自己的子图,方法是单独绘制每个图层及其特定点数据集,然后再使用print.trellis.我试过了,但是与带有栅格堆栈的levelplot相比,结果差很多.
I would like to avoid making my own subplot by plotting each layer with its' specific point dataset alone followed by using print.trellis. I tried it, but the result is just inferior compared to levelplot with raster stacks.
关于如何实现这一目标的任何想法?
Any idea on how to achieve that?
推荐答案
使用panel.number
功能,您可以根据所位于的面板将数据子集化:
With the panel.number
function you can subset your data according to the panel you are located:
pts <- list(pts1, pts2)
levelplot(r.stack) +
layer(sp.points(pts[[panel.number()]],
pch=20, cex=0.1, col="black"))
这篇关于在R中使用rasterVis在堆栈的每一层上绘制特定点数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!