Search code examples
rdictionarypanellatticecontour

R - Contour Map with multiple layers (lattice)


Here is my code and associated variable structures.

Correlation_Plot = contourplot(cor_Warra_SF_SST_JJA, region=TRUE, at=seq(-0.8, 0.8, 0.2), 
labels=FALSE, row.values=(lon_sst), column.values=lat_sst,
xlab='longitude', ylab='latitude')

Correlation_Plot = Correlation_Plot + layer({ ok <- (cor_Warra_SF_SST_JJA>0.6);
            panel.text(cor_Warra_SF_SST_JJA[ok]) })
Correlation_Plot

     # this is the longitude (from -179.5 to 179.5) , 360 data in total
    > str(lon_sst) 
     num [1:360(1d)] -180 -178 -178 -176 -176 ...

     # this is the latitude (from -89.5 to 89.5), 180 data in total 
    > str(lat_sst) 
     num [1:180(1d)] -89.5 -88.5 -87.5 -86.5 -85.5 -84.5 -83.5 -82.5 -81.5 -80.5 ...

     # This is data set corresponding to longitude and latitude  
     > dim(cor_Warra_SF_SST_JJA) 
       [1] 360 180

enter image description here

I tried to use layer() to show the label just for contour bigger than 0.6, but it doesn't work.

  1. Is it possible to increase the colour contrasts in the legend so it can be really clear what colour responds to what level. What are colour options, i can't find them?

  2. The most important is I want to draw a thicker black line for a specified contour interval (e.g. for +/- 0.2)? I think ican do it with layer() as well, but i am not sure what panel function should i use.

  3. Also, i tried to fill in the continent with a solid colour, but i can't find any thing with it. I have tried use map, but it doesn't work for lattice.

Thanks for your help.


Solution

  • Take a look at ?panel.levelplot for additional arguments to contourplot.

    1. You can use the col.regions argument, which will take either a vector of colours that you'd like to correspond to your intervals, or a color ramp function (e.g. below).

    2. Use a custom panel function, something like this (using a spatially autocorrelated dummy dataset generated using a method given on Santiago Beguería's blog). Use lpolygon to plot the map object:

      Generate dummy dataset:

      library(gstat)
      
      # create structure
      xy <- expand.grid(1:360, 1:180)
      names(xy) <- c('x','y')
      
      # define the gstat object (spatial model)
      g.dummy <- gstat(formula=z~1, locations=~x+y, dummy=T, beta=1,    
        model=vgm(psill=0.025,model='Exp',range=5), nmax=20)
      
      # make a simulations based on the gstat object
      yy <- predict(g.dummy, newdata=xy, nsim=1)
      gridded(yy) = ~x+y
      
      # scale to range [-1, 1]
      z <- matrix(yy@data[, 1], ncol=180)
      z.scalefac <- (max(z) - min(z)) / 2
      z <- -1 + (z - min(z)) / z.scalefac
      
    3. Plot:

      library(lattice)
      library(maps)
      
      lon_sst <- seq(-179.5, 179.5, 1)
      lat_sst <- seq(-89.5, 89.5, 1)
      
      colramp <- colorRampPalette(c('red', 'yellow', 'white', 'green', 'blue'))
      
      contourplot(z, xlim=c(100, 160), ylim=c(-80, -50), 
        at=seq(-1, 1, 0.2), region=TRUE, col.regions=colramp,
        row.values=lon_sst, column.values=lat_sst, labels=FALSE, 
        xlab='longitude', ylab='latitude',
        panel = function(at, region, ...) {
          panel.contourplot(at=at, region=TRUE,  ...)
          panel.contourplot(at=c(-0.2, 0.2), lwd=2, region=FALSE, ...)
          mp <- map("world", "antarctica", plot = FALSE, fill=TRUE)
          lpolygon(mp$x, mp$y, fill=TRUE, col='gray')
      })
      

    example output