Help with lattice panel function

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Help with lattice panel function

Naresh Gurbuxani
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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Help with lattice panel function

Christian Brandstätter
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 <[hidden email]>:

> 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]]
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Help with lattice panel function

Naresh Gurbuxani
Thanks for your lightening fast response.  This solution works.

Sent from my iPhone

> On Apr 29, 2015, at 7:27 AM, Brandstätter Christian <[hidden email]> 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 <[hidden email]>:
>> 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]]
>>
>> ______________________________________________
>> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Help with lattice panel function

Duncan Mackay-4
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: [hidden email]

-----Original Message-----
From: R-help [mailto:[hidden email]] On Behalf Of Naresh Gurbuxani
Sent: Wednesday, 29 April 2015 22:16
To: Brandstätter Christian
Cc: [hidden email]
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 <[hidden email]> 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 <[hidden email]>:
>> 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]]
>>
>> ______________________________________________
>> [hidden email] 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]]

______________________________________________
[hidden email] 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.

______________________________________________
[hidden email] 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.