Re: [R] Labeling a range of bars in barplot?

From: Marc Schwartz (via MN) <mschwartz_at_mn.rr.com>
Date: Wed 14 Dec 2005 - 05:05:50 EST

On Tue, 2005-12-13 at 10:53 +0000, Dan Bolser wrote:
> Hi, I am plotting a distribution of (ordered) values as a barplot. I
> would like to label groups of bars together to highlight aspects of the
> distribution. The label for the group should be the range of values in
> those bars.
>
> As this is hard to describe, here is an example;
>
>
> x <- rlnorm(50)*2
>
> barplot(sort(x,decreasing=T))
>
> y <- quantile(x, seq(0, 1, 0.2))
>
> y
>
> plot(diff(y))
>
>
>
> That last plot is to highlight that I want to label lots of the small
> columns together, and have a few more labels for the bigger columns
> (more densely labeled). I guess I will have to turn out my own labels
> using low level plotting functions, but I am stumped as to how to
> perform the calculation for label placement.
>
> I imagine drawing several line segments, one for each group of bars to
> be labeled together, and putting the range under each line segment as
> the label. Each line segment will sit under the group of bars that it
> covers.
>
> Thanks for any help with the above!
>
> Cheers,
> Dan.

Dan,

Here is a hint.

barplot() returns the bar midpoints:

mp <- barplot(sort(x, decreasing = TRUE))

> head(mp)

     [,1]

[1,]  0.7
[2,]  1.9
[3,]  3.1
[4,]  4.3
[5,]  5.5
[6,]  6.7

There will be one value in 'mp' for each bar in your series.

You can then use those values along the x axis to draw your line segments under the bars as you require, based upon the cut points you want to highlight.

To get the center of a given group of bars, you can use:

  mean(mp[start:end])

where 'start' and 'end' are the extreme bars in each of your groups.

Two other things that might be helpful. See ?cut and ?hist, noting the output in the latter when 'plot = FALSE'.

HTH, Marc Schwartz



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Wed Dec 14 07:03:47 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:41:36 EST