[R] Overlaying trellis xyplot on contourplot

Seth W Bigelow sbigelow at fs.fed.us
Thu Dec 13 22:20:42 CET 2007


Deepayan:

 Very nice, thanks for introducing me to a new resource. I will include the
entire, functioning example in the event others may find it useful.

--Seth

###### Sample code for overlaying data points on a contour graph, using
xyplot and contourplot ##################

library(lattice)

model <- function(a,b,c,X1,X2)            # provide model function for
contour plot
 {(exp(a + b*X1 + c*X2)) / (1 + exp(a + b*X1 + c*X2))}

g <- expand.grid(X1 = seq(0.38, 0.8,0.01), X2 = seq(0.03,0.99,0.03)) #
create gridded data for contour plot
a <- -37.61                               # Assign value to 'a' parameter
b <- 34.88                                # Assign value to 'b' parameter
c <- 28.44                                # Assign value to 'c' parameter
g$z<- model(a, b, c, g$X1,g$X2)           # Create variable z using gridded
data, model, and variables

# Data to superimpose as xyplot on the contourplot....
ph <-c(0.42,0.47,0.59,0.40)               # Create a vector of values under
variable 'ph'
phh <-c(0.76,0.81,0.82,0.71)              # Create vector of values for
variable 'phh'
d <- data.frame(ph,phh)                   # Group variables ph & phh in
data frame 'd'

contourplot(z ~ X1 * X2,
 data=g,
 contour=TRUE,
 xlim=c(0.38,0.8), ylim=c(0.401,0.999), zlim=c(0,1), # Set Axis Ranges
 xlab="p(H)", ylab="p(H|H)",              # Set axis labels
 region = TRUE,
 cuts=10,
 panel = function(x,y,subscripts,...){
 panel.contourplot(x,y,subscripts,...)
 panel.xyplot(d$ph,d$phh)}
 )

#### End ##############################



More information about the R-help mailing list