[R] Clusters of adjacent bars in barchart()
Paul Murrell
p.murrell at auckland.ac.nz
Tue Jun 18 01:04:54 CEST 2002
Hi
> Can I create a barchart in lattice with clusters of adjacent bars, which is
> possible with barplot() when beside=TRUE? How can I
> accomplish the grouping in lattice that is done with
> inputting a matrix to barplot()?
(after a quick look at the code) I don't think barchart() will do this.
Deepayan (the lattice author) is away at the moment, but I have cc'ed
him in case I have missed something obvious.
In the meantime, if you're interested, below is a panel function that
might do what you want. It's a bit of a draft really because it ignores
the box.ratio and horizontal arguments. Furthermore, it relies on the
current way that a matrix x argument gets passed to the panel function,
which is more by accident than design at this stage I think -- the
important point is that this may change in the future.
Anyway, hope it's of some help.
Paul
### customised barchart panel function
mybarchartpanel <- function(x, y,
# Choose some vaguely rational default
colours
col=trellis.par.get("regions")$col[seq(1,
length(trellis.par.get("regions")$col),
length=ncol)],
...) {
yscale <- current.viewport()$yscale
# For each x value
for (i in unique(x)) {
# Allocate a region for multiple side-by-side bars
# The 1/(1.5*ny) is a rough calculation to make sure the
# regions don't overlap
push.viewport(viewport(x=unit(i, "native"),
width=unit(1/(1.5*ny), "npc"),
# Allocate a sub-region for each
# individual side-by-side bar
layout=grid.layout(1, ncol)))
yy <- y[x == i]
for (j in 1:ncol) {
# Draw each individual side-by-side bar
push.viewport(viewport(layout.pos.col=j, yscale=yscale))
grid.rect(y=unit(0, "native"),
height=unit(yy[j], "native"),
just=c("centre", "bottom"),
gp=gpar(fill=col[j]))
pop.viewport()
}
pop.viewport()
}
}
# A simple example of a single barchart
# The data are not very interesting, but they're good
# for checking that it's working :)
# NOTE that I have to rep() the x values so that x is the same
# length as y
ncol <- 3
ny <- 8
y <- matrix(1:(ny*ncol), ncol=ncol)
x <- factor(rep(letters[1:ny], ncol))
barchart(y ~ x, panel=mybarchartpanel)
# A more complicated example with multiple panels
ng <- 4
y <- matrix(1:(ny*ng*ncol), ncol=ncol)
x <- factor(rep(letters[1:ny], ncol*ng))
g <- factor(rep(1:ng, rep(ny, ng)))
barchart(y ~ x | g, panel=mybarchartpanel)
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list