0

enter image description hereI am experiencing problems to fit label in the right place under the histogram bins as you can see in the pic. I am using this code:

![# Category names
my.names <- c("test1", "test2", "test3", "test4", "test5", "test6")

# Example data
data <- c(191,33,12,254,22,74)

# Normalize the example data as a percentage of the total
data.norm <- data / sum(data)

# Use barplot to plot the results
barplot(data.norm, beside=TRUE, col=c("grey10","grey20","grey30","grey40","grey50","grey60"))
text(1:6, par("usr")[3], labels=my.names, srt=45, pos=2, xpd=TRUE,offset=0.01)][2]

Please help Regards

user1997567
  • 439
  • 4
  • 19
  • Bars are not drawn at 1:6. You can derive the actual midpoints from the barplot function. Have a look at [this](http://stackoverflow.com/a/25037416/2516066) answer. – koekenbakker Sep 03 '14 at 15:38
  • Perhaps this is a better dup since it refers directly to `barplot` rather than `hist`: http://stackoverflow.com/questions/18634431/r-in-barplot-midpoints-are-not-centered-w-r-t-bars. I didn't close myself just in case someone knows of an ever better dup. – MrFlick Sep 03 '14 at 15:42

1 Answers1

1

The x axis values are not necessarily interger values. The barplot function returns the x values used during plotting. Try

bp<-barplot(data.norm, beside=TRUE, col=c("grey10","grey20","grey30","grey40","grey50","grey60"))
text(bp, par("usr")[3], labels=my.names, srt=45, pos=2, xpd=TRUE,offset=0.01)

instead.

enter image description here

MrFlick
  • 195,160
  • 17
  • 277
  • 295