R crop raster data and set axis limits

爱⌒轻易说出口 提交于 2019-11-30 01:32:11

After downloading the data file, I can read directly with raster. I choose band 221 that (if I am not wrong) it is what you need according to this table:

library("raster")
t2mc <- raster('gfs.grb', band=221)

> t2mc
class       : RasterLayer 
band        : 221  (of  315  bands)
dimensions  : 361, 720, 259920  (nrow, ncol, ncell)
resolution  : 0.5, 0.5  (x, y)
extent      : -0.25, 359.75, -90.25, 90.25  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +a=6371229 +b=6371229 +no_defs 
data source : /home/oscar/gfs.grb 
names       : gfs 

You don't need the whole extent so you use crop to get the desired extent:

e <- extent(-40,40,20,90)
tt <- crop(t2mc,e)

I have tried to display the tt raster with plot without success. However, it works correctly with spplot if you use a different extent (89.5 instead of 90):

e <- extent(-40,40,20,89.5)
tt <- crop(t2mc,e)

spplot(tt)

Now we have to add the administrative boundaries:

library(maps)
library(mapdata)
library(maptools)

ext <- as.vector(e)
boundaries <- map('worldHires',
                  xlim=ext[1:2], ylim=ext[3:4],
                  plot=FALSE)
boundaries <- map2SpatialLines(boundaries,
                               proj4string=CRS(projection(tt)))

and change the palette:

rgb.palette <- colorRampPalette(c("snow1","snow2","snow3","seagreen","orange","firebrick"),
                                space = "rgb")

spplot(tt, col.regions=rgb.palette,
       colorkey=list(height=0.3),
       sp.layout=list('sp.lines', boundaries, lwd=0.5))

If you prefer the latticeExtra::layer approach, you can achieve a similar result with this code:

library(rasterVis)
levelplot(tt, col.regions=rgb.palette,
          colorkey=list(height=.3)) +
    layer(sp.lines(boundaries, lwd=0.5))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!