[R] Help on BarPlot
Marc Schwartz
marc_schwartz at comcast.net
Mon Feb 9 22:51:18 CET 2009
on 02/09/2009 03:21 PM Steve Sidney wrote:
> Dear all
>
> As a new user of R, can someone please help me with the following
>
> I have created a programme to analyse laboratory data and one of the
> graphs is a bar plot of 'Z' scores.
>
> On the bar plot I am using the following line to plot some results
>
> barplot (zb[,c("ZBW")], ylim = c(-6,6),
> names.arg=zb[,c("LabNo")],xlab="Lab Code Number", cex.names = .5 ,
> axis.lty=1 , main = " Total Coliform ")
>
> What I would like to do is move the xlab parameter to sit on top or
> on each bar and not on the x-axis at y=0.
>
> Any help would be appreciated.
If the 'Z' scores are the typical standard deviation based continuous Z
scores, rather than a barplot, I would recommend a dotchart or point
based plot. Barplots are best suited for count/frequency based data. See
?dotchart.
With respect to the barplot, the key piece of information to know is
that barplot() returns the bar midpoints, which you need to know for
subsequent text placement. This is described in ?barplot.
Something like this should get you there:
# For reproducibility
set.seed(1)
# Generate a sample of LETTERS
L <- sample(LETTERS[1:5], 100, replace = TRUE)
# Create a frequency table
TL <- table(L)
> TL
L
A B C D E
13 25 19 26 17
# Create the barplot, set ylim to make room for the text
# set names.arg so that nothing appears below the x axis
mp <- barplot(TL, ylim = c(0, 30), names.arg = NA)
# What's in mp?
> mp
[,1]
[1,] 0.7
[2,] 1.9
[3,] 3.1
[4,] 4.3
[5,] 5.5
# Now draw the text at x = mp; y = TL positions.
# Set 'pos' to move the values above the bars
text(mp, TL, labels = names(TL), pos = 3)
# If you wanted the counts above the bars instead of the labels:
text(mp, TL, labels = TL, pos = 3)
HTH,
Marc Schwartz
More information about the R-help
mailing list