将透明矩形添加到spplot

时间:2018-07-02 09:43:43

标签: r lattice sp

可复制的示例(来自rspatial的fig01):

library(sp)
library(lattice)

data(meuse)
coordinates(meuse)=~x+y
spplot(meuse, "zinc", do.log = TRUE,
       key.space=list(x=0.2,y=0.9,corner=c(0,1)),
       scales=list(draw=T))

现在,我想用透明矩形绘制相同的图,该矩形的边界是根据数据坐标的“本机”单位指定的(与this答案不同):

spplot(meuse, "zinc", do.log = TRUE,
       key.space=list(x=0.2,y=0.9,corner=c(0,1)),
       scales=list(draw=T),
       panel=function(){
         panel.rect(xleft=180000, ybottom=330000,
                    xright=181000, ytop=330500, alpha=1)
       })

但是,我最终得到了一个没有数据点的新图:

fig01_with_panelrect

1 个答案:

答案 0 :(得分:2)

在面板功能中,还应在最后调用panel.points,以便在矩形之后绘制数据点:

spplot(meuse, "zinc", do.log = TRUE,
       key.space=list(x=0.2,y=0.9,corner=c(0,1)),
       scales=list(draw=T),
       panel=function(x, y, ...){
         panel.rect(xleft=180000, ybottom=330000,
                    xright=181000, ytop=330500, alpha=1)
         panel.points(x, y, ...)
       })

fig01_with_panelrect_and_panelpoints