Re: [R] combine barchart and xyplot in lattice

2023-11-18 Thread Naresh Gurbuxani
I converted to factor because, in barchart, x-axis seems to be factor only.  
Without factor, x labels are 1, 2, 3, …

Solution 1 works for me.  If there is a method for barchart, I am interested in 
looking at that as well.

Thanks,
Naresh
Sent from my iPhone

> On Nov 18, 2023, at 10:09 AM, Deepayan Sarkar  
> wrote:
> 
> On Sat, 18 Nov 2023 at 06:44, Naresh Gurbuxani
>  wrote:
>> 
>> In below graph, I would like to add two vertical lines using
>> panel.abline().  Is this possible?
> 
> I assume you want the 'v' variable in panel.abline() to be interpreted
> in the context of your x-axis, which here represents a factor
> variable. Unless two factor variables have the same levels, their
> values don't really mean the same thing. So either you need to specify
> the levels, or make the axis numeric:
> 
> # option 1 - factor
> xyplot(count ~ as.factor(hour), type = "l", lwd = 2,
>   col = 2, data = vehicles,
>   panel = function(x, y, ...) {
>   panel.xyplot(x, y, ...)
>   panel.abline(v = factor(c("9", "17"), levels = levels(x)),
>lty = 2, col = "gray")
>   })
> 
> # option 2 - numeric
> xyplot(count ~ hour, type = "l", lwd = 2,
>   col = 2, data = vehicles,
>   panel = function(x, y, ...) {
>   panel.xyplot(x, y, ...)
>   panel.abline(v = c(9, 17), lty = 2, col = "gray")
>   })
> 
> Best,
> -Deepayan
> 
>> 
>> Thanks,
>> Naresh
>> 
>> mydf <- data.frame(hour = rep(6:20, 2),
>> traffic = c(round(dnorm(6:20, 9, 3) * 1), round(dnorm(6:20, 17, 4) *
>> 1)),
>> direction = rep(c("inbound", "outbound"), c(15, 15)))
>> 
>> vehicles <- data.frame(hour = 6:20,
>> count = c(100, 120, 140, 125, 105, 80, 70, 75, 80, 100, 110, 120, 115,
>> 110, 100))
>> 
>> library(lattice)
>> library(latticeExtra)
>> 
>> # This works
>> mybars <- barchart(traffic ~ as.factor(hour), groups = direction,
>> stack = TRUE, horizontal = FALSE, data = mydf,
>> auto.key = list(columns = 2, space = "bottom"), xlab = "hour")
>> 
>> mylines <- xyplot(count ~ as.factor(hour), type = "l", lwd = 2,
>> col = 2, data = vehicles)
>> 
>> mybars + mylines
>> 
>> # This does not work.  Lines are not correctly placed.
>> mylines <- xyplot(count ~ as.factor(hour), type = "l", lwd = 2,
>> col = 2, data = vehicles, panel = function(x, y, ...) {
>> panel.xyplot(x, y, ...)
>> panel.abliine(v = as.factor(c(9, 17)), lty = 2, col = "gray")
>> })
>> __
>> R-help@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.
__
R-help@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.


Re: [R] combine barchart and xyplot in lattice

2023-11-18 Thread Deepayan Sarkar
On Sat, 18 Nov 2023 at 06:44, Naresh Gurbuxani
 wrote:
>
> In below graph, I would like to add two vertical lines using
> panel.abline().  Is this possible?

I assume you want the 'v' variable in panel.abline() to be interpreted
in the context of your x-axis, which here represents a factor
variable. Unless two factor variables have the same levels, their
values don't really mean the same thing. So either you need to specify
the levels, or make the axis numeric:

# option 1 - factor
xyplot(count ~ as.factor(hour), type = "l", lwd = 2,
   col = 2, data = vehicles,
   panel = function(x, y, ...) {
   panel.xyplot(x, y, ...)
   panel.abline(v = factor(c("9", "17"), levels = levels(x)),
lty = 2, col = "gray")
   })

# option 2 - numeric
xyplot(count ~ hour, type = "l", lwd = 2,
   col = 2, data = vehicles,
   panel = function(x, y, ...) {
   panel.xyplot(x, y, ...)
   panel.abline(v = c(9, 17), lty = 2, col = "gray")
   })

Best,
-Deepayan

>
> Thanks,
> Naresh
>
> mydf <- data.frame(hour = rep(6:20, 2),
> traffic = c(round(dnorm(6:20, 9, 3) * 1), round(dnorm(6:20, 17, 4) *
> 1)),
> direction = rep(c("inbound", "outbound"), c(15, 15)))
>
> vehicles <- data.frame(hour = 6:20,
> count = c(100, 120, 140, 125, 105, 80, 70, 75, 80, 100, 110, 120, 115,
> 110, 100))
>
> library(lattice)
> library(latticeExtra)
>
> # This works
> mybars <- barchart(traffic ~ as.factor(hour), groups = direction,
> stack = TRUE, horizontal = FALSE, data = mydf,
> auto.key = list(columns = 2, space = "bottom"), xlab = "hour")
>
> mylines <- xyplot(count ~ as.factor(hour), type = "l", lwd = 2,
> col = 2, data = vehicles)
>
> mybars + mylines
>
> # This does not work.  Lines are not correctly placed.
> mylines <- xyplot(count ~ as.factor(hour), type = "l", lwd = 2,
> col = 2, data = vehicles, panel = function(x, y, ...) {
> panel.xyplot(x, y, ...)
> panel.abliine(v = as.factor(c(9, 17)), lty = 2, col = "gray")
> })
> __
> R-help@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.

__
R-help@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.