[R] barplot(names.arg) versus axis(labels)
MSchwartz at medanalytics.com
Fri Nov 7 22:04:38 CET 2003
On Fri, 2003-11-07 at 14:29, Siddique, Amer wrote:
> Should I be able to use axis() on a barplot? i have a data.frame, the first
> 3 values of which are:
> > c[1:3,]
> median mean
> A1 56.5 58.50000
> A61 73.0 73.00000
> A62 63.0 63.00000
> > str(c)
> `data.frame': 19 obs. of 2 variables:
> $ median: num 56.5 73 63 161 51 55 44.5 22 54 49 ...
> $ mean : num 58.5 73.0 63.0 161.0 47.5 ...
> if I do barplot(median) and then try to label the bars with axis(), I get;
> > axis(1,,labels=rownames(c),font=4,cex=1)
You have an error in your call to axis().
You are missing the 'at' argument. As a result, the default values of
'at' are set to axTicks(1), which in this case is returning 5 values.
You have passed 19 values to the 'labels' argument in axis(). Hence the
error message below.
> Error in axis(side, at, labels, tick, line, pos, outer, font, vfont, lty, :
> location and label lengths differ, 5 != 19
> even though
> barplot(median, names.arg=rownames(c)) works and
> > length(rownames(c))
>  19
In the above call to barplot(), you have specified the bar names, which
will be matched within the function to the number of bars, thus it
> also when I attempt to place the value of the observation
> above the bar it does not space properly across all bars:
> do i need to explicitly state an x-pos for the co-ord argument here?
Yes. You get the proper x axis values by calling barplot() in the
mp <- barplot(....)
In this case, barplot() returns the bar midpoints and assigns them to
Once you have that information, you can call text() with the x values
set to 'mp'.
See ?barplot for examples and you may wish to review the most recent R
News, where there is an article on R's base graphics in R Help Desk.
Just beware though....the author of that article has been known indulge
in single malt scotches on Friday afternoons... ;-)
More information about the R-help