As per this discussion, here is one way of doing this: it consists in breaking the SpatialPolygonsDataFrame into one single matrix of polygons coordinates separated by NAs. Then plotting this on the levelplot using panel.polygon
.
library(maptools)
a <- matrix(rnorm(360*180),nrow=360,ncol=180) #Some random data (=your airtemp)
b <- readShapeSpatial("110-m_land.shp") #I used here a world map from Natural Earth.
And that's where the fun begins:
lb <- as(b, "SpatialPolygons")
llb <- slot(lb, "polygons")
B <- lapply(llb, slot, "Polygons") #At this point we have a list of SpatialPolygons
coords <- matrix(nrow=0, ncol=2)
for (i in seq_along(B)){
for (j in seq_along(B[[i]])) {
crds <- rbind(slot(B[[i]][[j]], "coords"), c(NA, NA)) #the NAs are used to separate the lines
coords <- rbind(coords, crds)
}
}
coords[,1] <- coords[,1]+180 # Because here your levelplot will be ranging from 0 to 360°
coords[,2] <- coords[,2]+90 # and 0 to 180° instead of -180 to 180 and -90 to 90
And then comes the plotting:
levelplot(a, panel=function(...){
panel.levelplot(...)
panel.polygon(coords)})
The idea in lattice is to define the plotting functions in argument panel
(see ?xyplot
for a complete explanation on the subject). The function for the levelplot itself is levelplot
.
Of course, in your case, it seems way simpler to plot this using base
graphics:
image(seq(-180,180,by=1),seq(-90,90,by=1),a)
plot(b, add=TRUE)
levelplot
is a lattice function,plot
is a base one, very hard to mix base and grid graphics. – Ignominious