Skip to content

contour lines for levelplot

2 messages · Marius Hofert, Deepayan Sarkar

#
Hi,

I would like to add contour lines to a (trellis/lattice-) levelplot.  
Sure, there is the "contour=TRUE" argument, but this uses  
"cuts=..." (which is usually chosen very high for my plots. I guess  
cuts=99 is the best you can do (?)) for plotting the contour lines.  
Furthermore, I do not like the numbering of the contour lines this  
way. Therefore, I tried to add a standard contour plot. There are 2  
problems to do this. First, as I would like to have the levelplot  
with a colorkey, the contour lines are plotted over the colorkey (see  
example code below). I could not fix this problem by changing the  
aspect argument of the levelplot call or by working with xlim and  
ylim in the contour plot function. Second, I am not sure if the  
contour lines added this way actually represent the points they  
should (e.g. does the contour line labeled with 0.5 really hit the  
points of this height?). So, is there a simple way of how to add the  
contour lines (of the contour function) to the levelplot this way? If  
not, is it possible to plot the levelplot with cuts=99 but only draw  
the contour lines (with the option "contour=TRUE" in the levelplot  
call) at specified points? Is it possible to tell the levelplot  
function (with cuts=99) to plot a specified number of contour lines  
(say, only to plot 10 contour lines in the whole existing range of z- 
values)?

Thank you very much in advance

Marius
m_hofert at web.de

example code:

remove(list=objects())
x.1<-rep(seq(0,1,by=0.04),each=26)
x.2 <- 0.04*(0:25)
y.1<-rep(x.2,26)
y.2 <- 0.04*(0:25)
z.1 <-x.1*y.1
z.2<- matrix(unlist(z.1),ncol=26,byrow=TRUE)
library(lattice) #for trellis-like graphical plots
plot.new()
levelplot(z.1~x.1*y.1,xlim=c(0,1),ylim=c(0,1),aspect=1,scales=list 
(relation="free",tick.number=6),cuts=99,colorkey=list(tick.number=6))
contour(x.2,y.2,z.2,levels=seq(0,1,by=0.1),add=TRUE,col="black")
1 day later
#
I'm not sure what gave you the idea that there's a limit of 99 contour
lines, but no such limit is intended. You need to read the
documentation of panel.contourplot and use it in a panel function.
Hint:

library(lattice)

levelplot(volcano,
          panel = function(..., at, contour = FALSE, labels = NULL) {
              panel.levelplot(..., at = at, contour = contour, labels = labels)
              panel.contourplot(..., at = c(137, 141), contour = TRUE,
labels = FALSE)
          })

contour (and plot.new) are base graphics functions, they do not
(easily) mix with lattice functions (which are based on the grid
package).

Deepayan
On 2/12/06, Jan Marius Hofert <m_hofert at web.de> wrote: