# [R] Overlaying trellis xyplot on contourplot

Thu Dec 13 22:07:25 CET 2007

```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.

On 12/13/07, Seth W Bigelow <sbigelow at fs.fed.us> wrote:
>
> 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
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> and provide commented, minimal, self-contained, reproducible code.
>

--

```