Re: [R] rotated labels in barplot with beside=T and multiple groups

From: Marc Schwartz <MSchwartz_at_mn.rr.com>
Date: Wed 22 Feb 2006 - 00:58:23 EST

On Tue, 2006-02-21 at 14:07 +0100, Karin Lagesen wrote:
> I have a data set that I display using barplot. I don't know what you
> call it, but when I look at it, it looks like this:
>
>
> > lsu
>
> (0,0.1] (0.1,0.2] (0.2,0.3] (0.3,0.4] (0.4,0.5] (0.5,0.6]
> A 0.052631579 0.000000000 0.000000000 0.000000000 0.000000000 0.000000000
> B 0.000000000 0.000000000 0.001007049 0.003021148 0.000000000 0.000000000
> E 0.200000000 0.000000000 0.000000000 0.000000000 0.100000000 0.000000000
>
> (0.6,0.7] (0.7,0.8] (0.8,0.9] (0.9,1]
> A 0.000000000 0.000000000 0.000000000 0.947368421
> B 0.000000000 0.004028197 0.005035247 0.986908359
> E 0.100000000 0.000000000 0.100000000 0.500000000

It appears to be a matrix, comprised of the results of using cut() along with perhaps tapply() or similar on one or more continuous variables using a grouping variable. See ?cut and ?tapply.

> Now, trying the examples shown via the r-help mailing list I am trying
> to make a plot where each of the groups gets displayed in a
> histogram-like fashion upwards with the number 0.1, 0.2 and so forth
> underneath the group. What I do is the following:
>
>
>
> > par(mar = c(6, 4, 4, 2) + 0.1)
> > bplot = barplot(lsu, beside=TRUE, col=colors[1:length(lsu[,1])], ylim = c(0,1.0), xaxt = "n", xlab = "")
> > axis(side=1,at=bplot, labels=FALSE, tick=TRUE)
> NULL
> > nam=rep("a",10)
> > text(bplot, par("usr")[3] - 1.5, srt = 45, adj = 1, labels = nam, xpd = TRUE)
> NULL
> >
>
> The result is the bars pointing upwards, like I want, but I get one
> tickmark per bar, and no labels underneath. I want no tickmark, and
> one label per group.
>
> Any ideas as to what I am doing wrong?
>
> TIA,
>
> Karin

First, your code above has some errors.

The use of "col=colors[1:length(lsu[,1])]" does not work. It can either be:

  col=colors()[1:length(lsu[,1])]

or easier to read:

  colors()[1:nrow(lsu)]

Second, the offset (-1.5) you have in the call to text():

  par("usr")[3] - 1.5

puts the text labels well below the bottom of the plot, which is why they are not seen. You need to change it to:

  par("usr")[3] - .05

or a similarly reduced offset figure.

Thus, you can now use:

  par(mar = c(6, 4, 4, 2) + 0.1)

  bplot <- barplot(lsu, beside = TRUE,

                   col = colors()[1:nrow(lsu)], 
                   ylim = c(0, 1.0), xaxt = "n", xlab = "")

  axis(side = 1, at = bplot, labels = FALSE, tick = TRUE)

  nam <- rep("a", 10)

  text(bplot, par("usr")[3] - .05, srt = 45, adj = 1,

       labels = nam, xpd = TRUE)

In order to get group labels for each of the three bars, you need to pay attention to the "Value" section of ?barplot, which says:

     If 'beside' is true, use 'colMeans(mp)' for the midpoints of each
     _group_ of bars, see example.
  

Thus, if you want to place group labels on the plot use:

par(mar = c(6, 4, 4, 2) + 0.1)

bplot <- barplot(lsu, beside = TRUE,

                 col = colors()[1:nrow(lsu)], 
                 ylim = c(0, 1.0), xaxt = "n", xlab = "")

nam <- rep("a", 10)

# Here is the change. Use colMeans(bplot) text(colMeans(bplot), par("usr")[3] - .05, srt = 45, adj = 1,

     labels = nam, xpd = TRUE)

Do not use the call to axis() if you don't want the tick marks, since there is no other need for that function here.

If you do want to put tick marks at the midpoint of each group, you could use:

  axis(1, at = colMeans(bplot), labels = 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 Feb 22 01:02:44 2006

This archive was generated by hypermail 2.1.8 : Wed 22 Feb 2006 - 09:27:43 EST