[R] Problem with x labels of barplot

Duncan Murdoch murdoch.duncan at gmail.com
Thu Jul 14 14:27:07 CEST 2011


On 11-07-14 7:51 AM, Don wrote:
> Hello everyone,
> i am currently creating a barplot.
> This barplot takes a vector of ~200 datapoints.
>
> Each datapoint represents one bar.
> http://img96.imageshack.us/i/human1w.png/
> (Ok as you see, it is not only one barplot, but a series of barplots).
>
> Now, these barplots represent a human chromosome. This means they are
> ordered. For instance bar number 50, means position 50 in the human
> chromosome. I would like to have x-axis labels showing this.
> 0.......................50..........................100.......................150........................200...................250
> Yet i do not know how to accomplish this.
> If you use the normal plot function, these numbers on the xaxis are
> autogenerated, in case of a barplot, they are not, and i do not know how to
> create these labels.
>
> I would be happy about a solution.
>

The axis() function can draw whatever you ask for.  The tricky thing is 
to get the ticks in the right place, because a barplot doesn't have an 
obvious x-axis scale.  The secret is to save the result of the barplot 
call:  it contains the centres of the bars.

For example:

x <- rpois(200, 20)
centres <- barplot(x)
axis(1, at=centres[c(50, 100, 150)], labels=c(50, 100, 150))
box()

Duncan Murdoch



More information about the R-help mailing list