R: overlay chart on levelplot

I have a 'airtemp' raster file and a 'continents' polygonal shapefile. I would like to lay the "continents" on the "air cane", so the border of the "continents" is visible on top of the "air". I draw a raster file levelplot (trellis). First I read the polygon readShapeSpatial (maptools), and then plot .

The problem of levelplot and plot have different scales. plot has a smaller frame. Sorry, I don’t have a reproducible sample, but I think this is a fairly common problem for geophysicists. I found a similar question here:

http://r.789695.n4.nabble.com/overlaying-a-levelplot-on-a-map-plot-td2019419.html

but I do not quite understand the solution.

+4
source share
2 answers

You can overlay the shapefile with the +.trellis and layer functions from the latticeExtra package (which is automatically loaded using rasterVis ).

 library(raster) library(rasterVis) 

Let me build some data for playback. You can skip this part if you already have a raster file and shapefile.

 library(maps) library(mapdata) library(maptools) ## raster myRaster <- raster(xmn=-100, xmx=100, ymn=-60, ymx=60) myRaster <- init(myRaster, runif) ## polygon shapefile ext <- as.vector(extent(myRaster)) boundaries <- map('worldHires', fill=TRUE, xlim=ext[1:2], ylim=ext[3:4], plot=FALSE) ## read the map2SpatialPolygons help page for details IDs <- sapply(strsplit(boundaries$names, ":"), function(x) x[1]) bPols <- map2SpatialPolygons(boundaries, IDs=IDs, proj4string=CRS(projection(myRaster))) 

Now you draw a raster file with rasterVis::levelplot , a shapefile with sp::sp.polygons , and a general graphic is created with +.trellis and layer .

 levelplot(myRaster) + layer(sp.polygons(bPols)) 

overlay with transparent color

sp.polygons for fill uses the default transparent color, but you can change it:

 levelplot(myRaster) + layer(sp.polygons(bPols, fill='white', alpha=0.3)) 

overlay with white color

+9
source

In accordance with this discussion , here is one way to do this: it consists in splitting the SpatialPolygonsDataFrame into a single coordinate matrix of polygons separated by NS. Then build it at level 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 this is 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 there is a conspiracy:

 levelplot(a, panel=function(...){ panel.levelplot(...) panel.polygon(coords)}) 

The idea in the grid is to define the build functions in the panel argument (see ?xyplot for a full explanation on this). The function of the level itself is the levelplot .

enter image description here

Of course, in your case, it seems easier to do this using base graphics:

 image(seq(-180,180,by=1),seq(-90,90,by=1),a) plot(b, add=TRUE) 

enter image description here

+1
source

Source: https://habr.com/ru/post/1490812/


All Articles