Hi:

There are a few things wrong, I believe; hopefully my suggested fix is what
you're after...

On Thu, Oct 28, 2010 at 3:35 PM, Duncan Mackay <mackay@northnet.com.au>wrote:

> Hi  All
> I have regression coefficients from an experiment and I want to plot them
> in lattice using panel curve but I have run into error messages.
> I want an 3 panel conditioned plot of 2 curves of Treatment 2 in each panel
> conditioned by Treatment1, the example curve expression is x+value*x^2
> A rough toy example to give an idea of what I want is:
>
> Data:
> data = expand.grid(Treatment1 = LETTERS[1:3],Treatment2 = letters[1:2])
> data$value =seq(1.1,1.6,0.1)
> data
>  Treatment1 Treatment2 value
> 1          A          a   1.1
> 2          B          a   1.2
> 3          C          a   1.3
> 4          A          b   1.4
> 5          B          b   1.5
> 6          C          b   1.6
>
> xyplot(value|Treatment1, data = data,
>         groups = Treatment2
>         panel = function(value, groups){
>                       panel.curve(expr = x+ value*x^2, groups, from = -2,
> to = 2)
>                    }
> )
>
> Firstly, xyplot takes a y ~ x | group argument; you have no ~ in the call.
Secondly, you really have no x-y data to plot; essentially, value is the
parameter to distinguish the slope of the quadratic term in your function
for each factor combination. It seems to me that you need to create the data
from the function using your current data frame 'data' as the parameter
set.  This is a simple application of mapply (insert tongue in cheek here)
and a convenient way to call it is through the function mdply() in the plyr
package. Once we have that, then we can call xyplot() more conventionally.

# Package loads:
library(lattice)
library(plyr)

# Your input data
data = expand.grid(Treatment1 = LETTERS[1:3],Treatment2 = letters[1:2])
data$value =seq(1.1,1.6,0.1)

# Function to create a data frame consisting of the two treatments,
# the sequence of x values from your panel.curve call and the function
# values in y. This is designed for one row of your data frame data.
# Notice that the function arguments match the names in the data frame.
# This is intentional.
f <- function(Treatment1, Treatment2, value) {
     x <- seq(from = -2, to = 2, by = 0.02)
     y <- x * value * x^2
     data.frame(Treatment1, Treatment2, x, y)
  }

# Call the function f on each row of data, and 'automagically'
# rbind the results together into a single data frame (note: mdply
# means 'mapply the rows of the parameter set data on the function
# f and output the results to a data frame')
dd <- mdply(data,  f)

# Run xyplot() on the newly created data frame using the user-defined
# key mykey:

mykey <- list(corner = c(0.8, 0.8),
               title = 'Treatment2',
               cex.title = 1.2,
               text = list(levels(dd$Treatment2), cex = 1),
               lines = list(lty = 1, col = c('red', 'blue'), lwd = 1.4 ))
xyplot(y ~ x | Treatment1, data = dd, groups = Treatment2,
        type = 'l', col.line = c('red', 'blue'), lwd = 1.4, key = mykey)

HTH,
Dennis


I tried my one panel function but came up with more error messages so have
> omitted it.
>
> Regards
>
> Duncan
>
> Duncan Mackay
> Department of Agronomy and Soil Science
> University of New England
> ARMIDALE NSW 2351
> Email home: mackay@northnet.com.au
>
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]

