[R] Help with lattice panel function

Duncan Mackay dulcalma at bigpond.com
Wed Apr 29 15:56:45 CEST 2015


Did you want both "groups" to have loess?

xyplot(x + max.x ~ date, data = my.newdf, ylab = "x",
       panel = function(x, y, x2, ...){
         panel.xyplot(x, y, type = "l")
         panel.loess(as.numeric(my.newdf$date), my.newdf$max.x, lty = 2)
         #panel.xyplot(x, y2, type = "l")
       })

xyplot(x + max.x ~ date, data = my.newdf, ylab = "x",
    panel = function(x, y, ...){
   	panel.xyplot(x, y, type = "l")
   	panel.loess(x, y, lty = 2)
   })
# equivalent to above
xyplot(x + max.x ~ date, data = my.newdf, ylab = "x",
    outer = F,
    panel = function(x, y, ...){
   	panel.xyplot(x, y, type = "l")
   	panel.loess(x, y, lty = 2)
   })

# both groups with loess
xyplot(x + max.x ~ date, data = my.newdf, ylab = "x",
       outer = F,
       panel = panel.superpose,
    panel.groups = function(x, y, ...){
   	panel.xyplot(x, y, type = "l")
   	panel.loess(x, y, lty = 2)
   })

Duncan

Duncan Mackay
Department of Agronomy and Soil Science
University of New England
Armidale NSW 2351
Email: home: mackay at northnet.com.au

-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Naresh Gurbuxani
Sent: Wednesday, 29 April 2015 22:16
To: Brandstätter Christian
Cc: r-help at R-project.org
Subject: Re: [R] Help with lattice panel function

Thanks for your lightening fast response.  This solution works.

Sent from my iPhone

> On Apr 29, 2015, at 7:27 AM, Brandstätter Christian <bran.chri at gmail.com> wrote:
> 
> This worked for me. It is btw. quite confusing to name your y-variable x. 
> I think part of the problem arised from the date format. 
> 
> xyplot(x + max.x ~ date, data = my.newdf, ylab = "x",
>        panel = function(x, y, x2, ...){
>          panel.xyplot(x, y, type = "l")
>          panel.loess(as.numeric(my.newdf$date), my.newdf$max.x, lty = 2)
>          #panel.xyplot(x, y2, type = "l")
>        })
> 
> 
> 2015-04-29 13:09 GMT+02:00 Naresh Gurbuxani <naresh_gurbuxani at hotmail.com>:
>> I want to plot multiple variables in xyplot, but plot loess trend for only one of these variables.  My problem is that the last command below does not give the desired result.
>> Any help will be gratefully received.
>> Thanks,Naresh
>> my.df <- data.frame(date = as.numeric(as.Date("2015-01-01")) + 0:49, x = rnorm(50))
>> my.df$date <- as.Date(my.df$date, origin = as.Date("1970-01-01"))
>> 
>> 
>> library(zoo)
>> x <- zoo(my.df[,"x"], my.df[,"date"])
>> max.x <- rollapply(x, 10, max, align = "right")
>> x <- merge(x, max.x)
>> my.newdf <- data.frame(x)
>> my.newdf$date <- as.Date(row.names(my.newdf))
>> 
>> 
>> library(lattice)# This works as expected
>> xyplot(x + max.x ~ date, data = my.newdf, type = "l",
>>    auto.key = list(columns = 2, points = FALSE, lines = TRUE), ylab = "x")
>>    # This does not work
>> xyplot(x ~ date, data = my.newdf, y2 = max.x, ylab = "x",
>>    panel = function(x, y, x2, ...){
>>         panel.xyplot(x, y, type = "l")
>>         panel.loess(x, y, lty = 2)
>>         panel.xyplot(x, y2, type = "l")
>>    })
>>         [[alternative HTML version deleted]]
>> 
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> 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]]

______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
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