Search code examples
rplotraster

How to put latitude on Y axis in hovmoller plot?


I am plotting hovmoller plot for three rasters but the function plots the latitude on the x axis and the time on the y axis. normally (much easier to read) latitude is on the Y axis. Reproducible example:

library("rasterVis")
r <- raster(nrows=10, ncols=10)
r <- setValues(r, 1:ncell(r))
r1 <- raster(nrows=10, ncols=10)
r1 <- setValues(r1, 1:ncell(r))
r2 <- raster(nrows=10, ncols=10)
r2 <- setValues(r2, 1:ncell(r))
St=stack(r,r1,r2)
idx <- seq(as.Date('2008-01-15'), as.Date('2008-1-17'), 'day')
SISmm <- setZ(St, idx)
hovmoller(SISmm, contour=FALSE, panel=panel.levelplot.raster,
          yscale.components=yscale.raster.subticks,
          interpolate=TRUE, par.settings=RdBuTheme)

This produces: example plot

So I want the time to be on the X axis and latitude on the Y axis!


Solution

  • That layout is not implemented in the rasterVis::hovmoller method. Only the common arrangement (time on the y-axis and latitude/longitude on the x-axis) is available at the moment.

    You can try a workaround using the most important parts of the function code. For your example, you have to create a RasterLayer with the latitude values. This RasterLayer is used by zonal to aggregate your RasterStack:

    library('raster')
    
    dirLayer <- init(SISmm, v='y')
    z <- zonal(SISmm, dirLayer, FUN='mean', digits=2)
    

    The result is a matrix which is converted to a data.frame to be displayed with lattice::levelplot.

    ## Time on the x-axis, latitude on the y-axis
    dat <- expand.grid(y=z[,1], x=idx)
    dat$z <- as.vector(z[,-1], mode='numeric')
    
    levelplot(z ~ x*y, data=dat,
              xlab='Time', ylab='Latitude',
              panel=panel.levelplot.raster,
              interpolate=TRUE,
              par.settings=RdBuTheme())
    

    enter image description here