Walmes Marques Zeviani
2010-Feb-20 13:22 UTC
[R] How get the single bar x coordinate in barchart when groups is used?
Hi all, I am using barchart() to plot values above the bars. When using groups argument we get bars grouped arround a given x level. By placing values above this bars we need to know the respective x coordinates. How can I get it? require(lattice) da <- expand.grid(x=1:5, z=1:3, w=1:2) da$y <- rpois(da$x, lambda=23) barchart(y~x|w, groups=z, data=da, horizontal=FALSE, panel=function(x, y, subscripts, groups, ...){ panel.barchart(x, y, subscripts=subscripts, groups=groups, ...) d <- 0.22 # <------ how obtain "d" or coordinates? panel.text(x+c(-d,0,d), y, label=y, pos=3) }) Thanks in advance. Walmes Zeviani, Lavras - MG, Brasil. _________________________________________________________________ os. dium=Tagline&utm_campaign=InfuseSocial [[alternative HTML version deleted]]
Peter Ehlers
2010-Feb-21 20:06 UTC
[R] How get the single bar x coordinate in barchart when groups is used?
On 2010-02-20 6:22, Walmes Marques Zeviani wrote:> > Hi all, > > I am using barchart() to plot values above the bars. When using groups argument we get bars grouped arround a given x level. By placing values above this bars we need to know the respective x coordinates. How can I get it? > > require(lattice) > da<- expand.grid(x=1:5, z=1:3, w=1:2) > da$y<- rpois(da$x, lambda=23) > > barchart(y~x|w, groups=z, data=da, horizontal=FALSE, > panel=function(x, y, subscripts, groups, ...){ > panel.barchart(x, y, subscripts=subscripts, groups=groups, ...) > d<- 0.22 #<------ how obtain "d" or coordinates? > panel.text(x+c(-d,0,d), y, label=y, pos=3) > }) >The width of each bar is 'd'; the width of the space between bars is 's'; the distance from the start of one group to the start of the next group is 3*d + s = 1 unit on the x-scale; Now we need to know the relationship of 's' to 'd': 3*d/s = 2; Hence 3*d + 3*d/2 = 1 => d = 2/9 (so your 0.22 was spot-on). How do we know that 3*d/s = 2? That's the default box.ratio value. See what happens if you add box.ratio=3 (or whatever) to your call. -Peter Ehlers> Thanks in advance. > Walmes Zeviani, Lavras - MG, Brasil. > > _________________________________________________________________ > > os. > > dium=Tagline&utm_campaign=InfuseSocial > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > 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. > >-- Peter Ehlers University of Calgary