[R] Problem using with panel.average in Lattice package
Deepayan Sarkar
deepayan.sarkar at gmail.com
Wed Oct 7 20:16:09 CEST 2009
On Sun, Oct 4, 2009 at 3:20 PM, Peter Ehlers <ehlers at ucalgary.ca> wrote:
> Deepayan,
>
> Is something similar needed in the last example in ?xyplot? I don't
> see panel.linejoin producing any lines.
You are right (I never noticed because that example doesn't get run).
I'll hard-code the type="l" in panel.average/linejoin.
-Deepayan
> Regards,
> Peter Ehlers
>
> Deepayan Sarkar wrote:
>>
>> On Fri, Oct 2, 2009 at 8:03 PM, Mark Dalphin <mark.dalphin at peblnz.com>
>> wrote:
>>>
>>> Hi,
>>>
>>> I'm having a problem getting the panel.average function to work as I
>>> expect it to in a lattice plot. I wish to draw lines between the
>>> averages of groups of y-values at specific x-values. I have created a
>>> dataset below which is similar to my real data. I also show an example
>>> of using panel.loess in place of panel.average; it performs in a
>>> manner similar to what I want panel.average to do except it shows a
>>> loess line rather than a straight line connecting the means of the
>>> groups.
>>>
>>> Please see my coded examples, below.
>>>
>>> Regards,
>>> Mark Dalphin
>>>
>>> =================================================================
>>> My system information:
>>>
>>>> library(lattice)
>>>> print(sessionInfo())
>>>
>>> R version 2.9.1 (2009-06-26) i686-pc-linux-gnu
>>> locale:
>>>
>>> LC_CTYPE=en_NZ.UTF-8;LC_NUMERIC=C;LC_TIME=en_NZ.UTF-8;LC_COLLATE=en_NZ.UTF-8;
>>> LC_MONETARY=C;LC_MESSAGES=en_NZ.UTF-8;LC_PAPER=en_NZ.UTF-8;LC_NAME=C;
>>>
>>> LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=en_NZ.UTF-8;LC_IDENTIFICATION=C
>>>
>>> attached base packages:
>>> [1] stats graphics grDevices utils datasets methods base
>>> other attached packages:
>>> [1] lattice_0.17-25
>>>
>>> loaded via a namespace (and not attached):
>>> [1] grid_2.9.1 tools_2.9.1
>>>
>>> ##--------------------------------------------------------------
>>> ## This dataset is too complicated, but it does show the type of plot I
>>> want.
>>> ##
>>> ## Create a fake qPCR dataset: Eight 96-well plates over 4 days (2 per
>>> day),
>>> ## 2 genes per plate (multiplexed), and 4 "Hi" positive control and
>>> ## 4 "Lo" positive controls per plate.
>>> ## Create the experimental data; by rights it is all identical, expect
>>> for
>>> ## experimental errors with in days and between days.
>>> ## For this simulation, each gene will be given a base value.
>>> ## In qPCR the higher the "Ct" value, the lower the concentration.
>>> library(lattice) # Add for ease of cut-n-paste of this code
>>> date <- c('2009-09-07', '2009-09-08', '2009-09-10', '2009-09-14')
>>> probe <- c('Gene.A1', 'Gene.A2', 'Gene.B1', 'Gene.B2')
>>> conc <- c('Lo', 'Hi')
>>> base.lo <- c(Gene.A1=29, Gene.A2=25, Gene.B1=28, Gene.B2=31)
>>> base.hi <- base.lo - 8
>>> day.err <- c(Day.1=0, Day.2=1, Day.3=1.5, Day.4=1.0)
>>>
>>> d <- data.frame()
>>> for(i in seq(along=date)) {
>>> for(j in seq(along=probe)) {
>>> for(k in seq(along=conc)) {
>>> d <- rbind(d, data.frame(Date=rep(date[i], length=4),
>>> Probe=rep(probe[j], length=4),
>>> Conc=rep(conc[k], length=4),
>>> Ct=rnorm(4, sd=0.5) + (k-1)*8 +
>>> base.hi[j] + day.err[i]
>>> ))
>>> }
>>> }
>>> }
>>> d$Date <- as.POSIXct(d$Date)
>>>
>>> ##--------------------------------------------------
>>> ## Example 1
>>> ## Print with LOESS line showing the 'means' for the groups.
>>> ## This is close, but I don't want a loess line; I want straight lines
>>> ## between mean values.
>>> print(xyplot(Ct ~ Date|Probe, group=Conc, data=d,
>>> panel="panel.superpose",
>>> panel.groups=function(x, y, ...) {
>>> panel.loess(x, y, ...)
>>> panel.xyplot(x, y, ...)
>>> },
>>> auto.key=TRUE))
>>>
>>> ##--------------------------------------------------
>>> ## Example 2
>>> ## Parallel construction to the loess example, above.
>>> ## Note the loss of the lines. The 'horizontal' default
>>> ## is different between 'panel.loess' and 'panel.average'.
>>> print(xyplot(Ct ~ Date|Probe, group=Conc, data=d,
>>> panel="panel.superpose",
>>> panel.groups=function(x, y, ...) {
>>> panel.average(x, y, horizontal=FALSE, ...)
>>> panel.xyplot(x, y, ...)
>>> },
>>> auto.key=TRUE))
>>
>> Unfortunately, the implicit type="p" argument in panel.superpose is
>> overriding the type="l" in panel.average (may be it should be
>> unmodifiable). So, you need
>>
>> print(xyplot(Ct ~ Date|Probe, group=Conc, data=d,
>> panel="panel.superpose",
>> panel.groups=function(x, y, ..., type) {
>> panel.average(x, y, ..., type = "l", horizontal = FALSE)
>> panel.xyplot(x, y, ..., type = type)
>> },
>> auto.key=TRUE))
>>
>> It's more common to use the syntactic sugar provided by panel.xyplot:
>>
>> print(xyplot(Ct ~ Date|Probe, group=Conc, data=d,
>> type = c("p", "a"),
>> auto.key=TRUE))
>>
>>
>>> ##--------------------------------------------------
>>> ## Example 3
>>> ## Don't pass along the '...' to the panel.average. Now I
>>> ## get lines, but not matching colours to the points.
>>> print(xyplot(Ct ~ Date|Probe, group=Conc, data=d,
>>> panel="panel.superpose",
>>> panel.groups=function(x, y, ...) {
>>> panel.average(x, y, horizontal=FALSE)
>>> panel.xyplot(x, y, ...)
>>> },
>>> auto.key=TRUE))
>>>
>>> ##**********************************************************************
>>> Main question:
>>>
>>> I want to create a plot that looks like Example 3, but with the
>>> coloured lines of Example 1. Suggestions? I've looked in RSiteSearch()
>>> for both "panel.average" and "panel.linejoin" but found nothing
>>> addressing
>>> this.
>>>
>>> Side question:
>>>
>>> I also read the source code to panel.average, panel.loess and
>>> panel.superpose. Which leads to a side question; how do I determine
>>> what parameters are being passed within '...'? I tried recreating my
>>> panel.groups function above as an explicit (non-anonymous) function
>>> and using debug() on it, but running formals() within that debugging
>>> session
>>> didn't seem to show me the huge list of arguments from
>>> panel.superpose, "do.call(panel.groups, args)", coming into my panel
>>> function. Is there some way to "see" these arguments?
>>
>> I usually put in
>>
>> str(list(...))
>>
>> in the function. For interactive debugging, you could have
>>
>> dots <- list(...)
>>
>> and then debug.
>>
>> -Deepayan
>>
>> ______________________________________________
>> R-help at 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.
>
More information about the R-help
mailing list