2

Reproducible example (fig01 from rspatial):

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))

Now, I would like to make the same plot with a transparent rectangle which boundaries are specified in terms of the "native" units of the data coordinates (different from this answer):

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)
       })

However, I end up with a new plot without the data points:

fig01_with_panelrect

zx8754
  • 52,746
  • 12
  • 114
  • 209
tflutre
  • 3,354
  • 9
  • 39
  • 53

1 Answers1

2

Inside the panel function, one should also call panel.points at the end so that data points are drawn after the rectangle:

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

zx8754
  • 52,746
  • 12
  • 114
  • 209
tflutre
  • 3,354
  • 9
  • 39
  • 53