-1

I would like to add a label on the x axis of my bar chart. Here is how I generated my bar chart in R:

x <- c(10,10,10,10,10)
y <- c(45.57, 8, 8, 14,0.5 )
barplot(y, x)
barplot(y, x, xaxt="n")

I know adding xaxt="n" allows me to edit my labels, but when I tried the following commands, the text was squeezed into a single side of the graph and not on the corresponding bar in x-axis:

 axis(side = 1, 
  at=1:5, 
  lab=c(   "LT", 
           "LB", 
           "LBN", 
           "CD",
           "MLE"
        ),
  las=3)
csgillespie
  • 54,386
  • 13
  • 138
  • 175

1 Answers1

0

That's because you did not read what the barplot function returns (besides having a side-effect of calling the graphics device.) It returns the locations in user coordinates, so use thos fo the aat values:

?barplot  # Read "Value" section
 barloc <- barplot(y, x, xaxt="n")
  axis(side = 1, 
   at=barloc, 
   lab=c(   "LT", 
            "LB", 
            "LBN", 
            "CD",
            "MLE"
         ),
   las=3)
IRTFM
  • 240,863
  • 19
  • 328
  • 451