为多个空间图形绘制相同的图例

时间:2018-09-18 18:33:38

标签: r plot raster sp labeling

我有一个for循环,可进行插值并按spplot绘制光栅图。 它只给出一个颜色条,但标签在每个图中均会改变。 您可以在下面看到区别。 enter image description here enter image description here

我想在ssplot中使用带有相同标签的相同颜色条,但是我无法根据相同的地图图例绘制图形。

这是代码的最后一部分

  

对于(...){

     
    

...

  
     

WElev.IDW = idw(公式= variable〜1,位置= spdf2,newdata = r.pts)

     

mypath <-file.path(“ C:”,“ ...”,paste(“ WElevMonth”,colnames(variable),“ .png”,sep =“”))

     

png(文件名= mypath)

     

print(spplot(WElev.IDW [“ var1.pred”]))

     

dev.off()}

1 个答案:

答案 0 :(得分:1)

stack()spplot RasterStack

s <- stack(raster1, raster2)
spplot(s)

结果情节将有一个共同的图例。


如果要使所有图相互独立,但要设置色阶的限制和中断,请使用at参数。向at传递中断向量。 See this post

首先,堆叠所有栅格,以便您可以快速计算堆叠的最小值和最大值。使用这些值来告知您的图例限制和中断。

# max and min of the stack
max_r  <- cellStats(s, max) # max of raster stack: legend upper limit
min_r  <- cellStats(s, min) # min of raster Stack: legend lower limit
breaks <- (max_r - min_r)/15 # increase denominator for more breaks
lab    <- seq(min_r, max_r, by = breaks) # create the vector of legend breaks

# now run your for loop and within `spplot`, set the same legend with `at`
for(i in 1:n){
  spplot(raster, at = lab)
  ...
}