简体   繁体   English

将 XY 点添加到 levelplot 生成的栅格地图

[英]Add XY points to raster map generated by levelplot

I have raster maps which are generated using the raster package in R. These raster layers can be visualized using the rasterVis package's levelplot function:我有使用 R 中的raster包生成的raster地图。这些光栅图层可以使用rasterVis包的levelplot函数进行可视化:

levelplot(rasterstack, layout=c(1, 2), 
          col.regions=colorRampPalette(c('darkred', 'red3', 'orange2', 'orange', 
                                         'yellow', 'lightskyblue', 'steelblue3', 
                                         'royalblue3', 'darkblue')))

Now, I would like to add some z values defined by xy cordinates to the levelplot map.现在,我想向 levelplot 地图添加一些由 xy 坐标定义的 z 值。 The dataframe containing z values has 4 columns.包含 z 值的数据框有 4 列。 Columns 1 & 2 contain x & y coordinates, column 3 contains z values for map 1 in layout(1, 1) and column 4 for layout(1, 2).第 1 和 2 列包含 x 和 y 坐标,第 3 列包含 layout(1, 1) 中地图 1 的 z 值,以及 layout(1, 2) 中第 4 列的 z 值。

The points per map should be added such that if z < 0.05, pch=2 and if z > 0.05, pch=3 .应该添加每个地图的点,如果 z < 0.05, pch=2 ,如果 z > 0.05, pch=3

I have searched the web and found a solution by Ripley but it does not work in my case:我在网上搜索并找到了 Ripley 的解决方案,但在我的情况下不起作用:

levelplot(rcp852, xlab = "", ylab = "",
          panel = function(x, y, subscripts, ...) {
            panel.levelplot(x, y, subscripts, ...)
            panel.xyplot(topo$x,topo$y, cex = 0.5, col = 1)
          }
)

I tried many other options but the points do not align with the map generated via levelplot .我尝试了许多其他选项,但这些点与通过levelplot生成的地图levelplot

layer is very convenient for this: layer对此非常方便:

library(raster)
library(rasterVis)
library(sp)

s <- stack(replicate(2, raster(matrix(runif(100), 10))))
xy <- data.frame(coordinates(sampleRandom(s, 10, sp=TRUE)),
                z1=runif(10), z2=runif(10))
coordinates(xy) <- ~x+y

levelplot(s, margin=FALSE, at=seq(0, 1, 0.05)) + 
  layer(sp.points(xy, pch=ifelse(xy$z1 < 0.5, 2, 3), cex=2, col=1), columns=1) +
  layer(sp.points(xy, pch=ifelse(xy$z2 < 0.5, 2, 3), cex=2, col=1), columns=2)

Note that the columns argument to layer ( rows also exists) specifies which panel(s) you want to add the layer to.请注意, layercolumns参数( rows也存在)指定要将图层添加到哪个面板。

在此处输入图片说明

So I had a dataframe with xy cordinates and so many z columns.所以我有一个带有 xy 坐标和这么多 z 列的数据框。 This is the final answer to get points added to my map thanks to @jbaums:感谢@jbaums,这是将积分添加到我的地图的最终答案:

s <- stack(raster1,raster2)
coordinates(SITES...TTEST) <- ~x+y # SpatialPointsDataFrame
levelplot(s, layout=c(1, 2), 
          col.regions=colorRampPalette(c("darkred", "red3", 
                                         "orange", "yellow", "lightskyblue", "royalblue3", 
                                         "darkblue")),
          at=seq(floor(39.15945) ,ceiling(51.85068), length.out=30), 
          par.strip.text=list(cex=0),scales=list(alternating=FALSE))+
  layer(sp.points(SITES...TTEST, pch=ifelse(SITES...TTEST$Precip_DJF6 < 0.05, 2, 3), cex=2, col=1), rows=1) +
  layer(sp.points(SITES...TTEST, pch=ifelse(SITES...TTEST$Precip_DJF6 < 0.05, 2, 3), cex=2, col=1), rows=2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM