[R] contour lines on levelplot?

Deepayan Sarkar deepayan at stat.wisc.edu
Mon Jun 7 08:52:19 CEST 2004


On Sunday 06 June 2004 20:40, Spencer Graves wrote:
>       With "image" and "contour", one can get both colors and lines
> to enhance the image of a contour plot.  What's the best way to do
> this with Lattice graphics?  The following is one ugly hack,
> producing the desired result after much trial and error (R 1.9.1
> alpha under Windows 2000):
>
> # setup
> DF <- expand.grid(x=1:3, y=1:3)
> DF$z <- (DF$x+DF$y)
>
> # Traditional "base" graphics:
> image(x=1:3, y=1:3, z=array(DF$z, dim=c(3,3)))
> contour(x=1:3, y=1:3, z=array(DF$z, dim=c(3,3)), add=T)
>
> # Lattice hack:
> lvlplt <- levelplot(z~x*y, DF)
> cont <- contourplot(z~x*y, DF)
> print(lvlplt, more=T)
> print(cont, position=c(0, 0, 0.9055, 1))
>
> ##################
>       By comparison, the same ugly hack in S-Plus required 0.952
> instead of 0.955.  Is this the best we can currently get with a
> modest effort?

There's certainly much easier ways to do this, namely 

contourplot(z~x*y, DF, region = TRUE)

or 

levelplot(z~x*y, DF, contour = TRUE)

the only restriction being that you would need to use the same 'at' 
vector. If you want them to differ, you need to write a small panel 
function of your own that makes use of panel.levelplot().

Deepayan




More information about the R-help mailing list