On Fri, 2003-07-18 at 13:41, Jay Pfaffman wrote:> Thanks for all the help on my previous histogram problem. I intend to
> summarize the solutions back to the list Real Soon Now, but first, I've
> got another problem.
>
> I've made a bar chart that reports means. I'd like to put the
number
> of observations on top of each bar. Here's what I've got:
>
> barplot((subset$x),
> col=grey(.5),
> ylab="Mean Engagement Rating",
> xlab="Comment Category",
> main="All Engagement Ratings",
> ylim=c(0,7),
> cex.names=.75,
> names.arg=mynames
> )
>
>
> for (i in 1:7){
> text(i,as.numeric(subset$x[i])+.5,counts$x[i])
> }
>
>
> This would seem to do the trick, but the numbers are not centered over
> the bars. They're a bit to the right on the bars on the left and
> almost right on the bars on the right. I don't understand why the
> scale is not the same.
>
> Thanks again for your help.
You need to get the bar midpoints (which are not integer values) and are
returned by barplot() 'silently'. You must assign these to a separate
variable to be used when calling barplot().
Thus, change your code to:
mp <- barplot(subset$x,
col=grey(.5),
ylab="Mean Engagement Rating",
xlab="Comment Category",
main="All Engagement Ratings",
ylim=c(0,7),
cex.names=.75,
names.arg=mynames
)
Now to place the bar values above the bar tops use:
text(mp, subset$x, labels = subset$x, pos = 3)
The 'pos = 3' argument, will place the text above the top of the bars.
Also, note that you do not need a for loop here. Remember, R is vector
based. A single call to text() will cycle through the values.
See ?barplot and ?text for more information.
HTH,
Marc Schwartz