[R] Lattice: vertical barchart
Sundar Dorai-Raj
sundar.dorai-raj at pdf.com
Tue Jul 10 16:51:31 CEST 2007
Michael Hoffman said the following on 7/10/2007 7:06 AM:
> barchart(Titanic, stack=F) produces a very nice horizontal barchart.
> Each panel has four groups of two bars.
>
> barchart(Titanic, stack=F, horizontal=F) doesn't produce the results I
> would have expected, as it produces this warning message:
>
> Warning message:
> y should be numeric in: bwplot.formula(x = as.formula(form), data =
> list(Class = c(1,
>
> And it results in each panel having 22 groups of 0-2 bars.
>
> How can I produce something just like the original except with the
> orientation changed?
>
> Thanks in advance.
>
Hi, Michael,
It seems that barchart.table doesn't allow the horizontal = FALSE
argument. With a slight modification to barchart.table this can be
accomplished. Also, I don't get a warning with your original code using
R-2.5.1 and lattice 0.16-1.
HTH,
--sundar
barchart.table <-
function (x, data = NULL, groups = TRUE, origin = 0, stack = TRUE,
horizontal = TRUE, ...) ## add horizontal argument
{
formula <- x
ocall <- sys.call(sys.parent())
if (!is.null(data))
warning("explicit 'data' specification ignored")
data <- as.data.frame(formula)
nms <- names(data)
freq <- which(nms == "Freq")
nms <- nms[-freq]
## SD: change formula if horizontal == FALSE
form <- if(horizontal) {
paste(nms[1], "Freq", sep = "~")
} else {
paste("Freq", nms[1], sep = "~")
}
## SD: end change
nms <- nms[-1]
len <- length(nms)
if (is.logical(groups) && groups && len > 0) {
groups <- as.name(nms[len])
nms <- nms[-len]
len <- length(nms)
}
else groups <- NULL
if (len > 0) {
rest <- paste(nms, collapse = "+")
form <- paste(form, rest, sep = "|")
}
ans <- barchart(as.formula(form), data, groups = eval(groups),
origin = origin, stack = stack, ...)
ans$call <- ocall
ans
}
barchart(Titanic, stack = FALSE)
barchart(Titanic, stack = FALSE, horizontal = FALSE)
More information about the R-help
mailing list