根据this discussion,这是执行此操作的一种方法:它将 SpatialPolygonsDataFrame 分解为一个由 NA 分隔的多边形坐标矩阵。然后使用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.
这就是乐趣的开始:
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
然后是绘图:
levelplot(a, panel=function(...){
panel.levelplot(...)
panel.polygon(coords)})
lattice 的想法是在参数panel 中定义绘图函数(有关该主题的完整说明,请参见?xyplot)。 levelplot 本身的函数是levelplot。
当然,在你的情况下,使用base 图形绘制它似乎更简单:
image(seq(-180,180,by=1),seq(-90,90,by=1),a)
plot(b, add=TRUE)