[R] combine barchart and xyplot in lattice
Naresh Gurbuxani
n@re@h_gurbux@n| @end|ng |rom hotm@||@com
Sat Nov 18 12:44:07 CET 2023
In below graph, I would like to add two vertical lines using
panel.abline(). Is this possible?
Thanks,
Naresh
mydf <- data.frame(hour = rep(6:20, 2),
traffic = c(round(dnorm(6:20, 9, 3) * 10000), round(dnorm(6:20, 17, 4) *
10000)),
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")
})
More information about the R-help
mailing list