[R] Overlaying a single contour from a new data array in levelplot

Deepayan Sarkar deepayan.sarkar at gmail.com
Wed Jul 25 01:12:25 CEST 2007

On 7/24/07, Jenny Barnes <jmb at mssl.ucl.ac.uk> wrote:
> Dear R-Help community,
> I am trying to overlay a single contour line over a correlation plot using
> levelplot in the lattice package. These are the two arrays:
> 1) a correlation plot over Africa - so each grid square is a different colour
> dependent on correlation - this is in an array: result_cor with dim[465,465]
> 2) a single contour line from a ***different data source*** - this is from data
> related to the p-values for the above correlation plot - I want to overlay only
> the 95% confidence contour. The p-values are stored in an array: result.p.values
> with same dimensions as above.
> I have read about using panel.levelplot and panel.contourplot in the R-help
> mailing list but I don't know the right way to call two different data arrays,
> can anybody help me please? I appreciate your time and help with this question.

I can think of a couple of different ways, but the simplest will
probably be to compute the single contour beforehand and add it after
the standard levelplot using a panel function.  E.g., using the
'volcano' data for both matrices:

## you need the explicit x and y arguments because
## the default is different from levelplot.

vcl <- contourLines(x = seq_len(nrow(volcano)),
                   y = seq_len(ncol(volcano)),
                   z = volcano,
                   levels = c(172, 182))

levelplot(volcano, add.cl = vcl,
          panel = function(..., add.cl) {
              lapply(add.cl, panel.polygon, border = 'red')


More information about the R-help mailing list