Search code examples
rplotscaleaxis-labels

R barplot axis scaling


I want to plot a barplot of some data with some x-axis labels but so far I just keep running into the same problem, as the axis scaling is completely off limits and therefore my labels are wrongly positioned below the bars. The most simple example I can think of:

x = c(1:81)
barplot(x)
axis(side=1,at=c(0,20,40,60,80),labels=c(20,40,60,80,100))

As you can see, the x-axis does not stretch along the whole plot but stops somewhere in between. It seems to me as if the problem is quite simple, but I somehow I am not able to fix it and I could not find any solution so far :(

Any help is greatly appreciated.


Solution

  • Not sure exactly what you want, but if it is to have the labels running from one end to the other evenly placed (but not necessarily accurately), then:

    x = c(1:81)
    bp <- barplot(x)  # returns centered x axis positions as matrix
    axis(side=1,
         at=bp[1+c(0,20,40,60,80)],  # Need to increment, R uses 1-based indices
         labels=c(20,40,60,80,100))
    

    The puzzle for me was why you wanted to label "20" at 0. But this is one way to do it.

    I think Ben is mostly correct about the x-axis being for (ordered) categories, but I don't really agree that the y values are intended to be categorical.