根据this discussion,这里有一种方法:它将SpatialPolygonsDataFrame分解为一个由NAs分隔的多边形坐标矩阵。然后使用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)})
晶格的想法是在争论panel
定义绘图功能(参见?xyplot
关于这个问题的完整解释)。 levelplot本身的函数是levelplot
。
当然,你的情况,似乎这样更简单的绘制该使用base
显卡:
image(seq(-180,180,by=1),seq(-90,90,by=1),a)
plot(b, add=TRUE)
回答说,那'levelplot'是晶格功能,'plot'是基础的,很难混合基础和网格图形。 – agstudy