R crop raster data and set axis limits

后端 未结 1 1731
余生分开走
余生分开走 2020-12-25 08:57

With your help in another thread I have managed to plot some global maps. First I convert meteorological GRIB2 data to Netcdf and then plot the global maps.

Now I w

相关标签:
1条回答
  • 2020-12-25 09:29

    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))
    

    spplot result

    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))
    
    0 讨论(0)
提交回复
热议问题