Skip to content

Overlaying trellis xyplot on contourplot

4 messages · Seth W Bigelow, Deepayan Sarkar, Hadley Wickham

#
Friends: I wish to overlay data points on a contour graph.  The following
example produces a nice contour plot, but I have not mastered the concept
of using panel functions to modify plots. Can someone show me how to
overlay the data points (given after contour plot statement) on the
contourplot?
--Seth


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.40, 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

contourplot(z ~ X1 * X2,                              # specify the basic
equation for the contour plot
 data=g,                                  # Specify the data frame to be
used
 contour=TRUE,                                  # Make sure it adds
contours
 xlim=c(0.4,0.8), ylim=c(0.401,0.999), zlim=c(0,1),         # Set axis
ranges
 xlab="p(H)", ylab="p(H|H)",                    # Add axis labels
 region = TRUE,                                 # Add nice colors
 cuts=10                                  # Specify number of contour
slices
 )

# 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'




Dr. Seth  W. Bigelow
Biologist, Sierra Nevada Research Center
Pacific Southwest Research Station, USDA Forest Service
Mailing address: 2121 2nd St Suite A101, Davis CA 95616
www.fs.fed.us/psw/programs/snrc/staff/bigelow
www.smbigelow.net
Phone: 530 759 1718
Fax: 530 747 0241
#
On 12/13/07, Seth W Bigelow <sbigelow at fs.fed.us> wrote:
MASS has an example (Figure 4.3); see

file.show(system.file("scripts/ch04.R", package = "MASS"))

-Deepayan
#
Hi Seth,

An alternative would be to use ggplot2, http://had.co.nz/ggplot2:

model <- function(a,b,c,X1,X2)  {
  (exp(a + b*X1 + c*X2)) / (1 + exp(a + b*X1 + c*X2))
}

g <- expand.grid(X1 = seq(0.40, 0.8,0.01), X2 = seq(0.03,0.99,0.03))
a <- -37.61
b <- 34.88
c <- 28.44
g$z<- model(a, b, c, g$X1,g$X2)

ph <-c(0.42,0.47,0.59,0.40)
phh <-c(0.76,0.81,0.82,0.71)
d <- data.frame(ph,phh)

library(ggplot2)
qplot(X1, X2, data = g, fill = z, geom="tile", xlab="p(H)", ylab="p(H|H)")+
 geom_contour(aes(z=z)) +
 geom_point(aes(x = ph, y = phh, fill = NULL), data=d)

Plots in ggplot2 have multiple layers which can have different data sources.

Hadley
On 12/13/07, Seth W Bigelow <sbigelow at fs.fed.us> wrote:

  
    
#
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 ##############################