[R] contour lines for levelplot

Jan Marius Hofert
Sun, 12 Feb 2006 08:52:25 -0000

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