Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Centering Values on Bars in Histogram in R

Tags:

r

Looking to have the values of x-axis plotted in the center of the bars in R.

Having issues finding a way to make this possible, code is below:

hist(sample_avg, breaks =7, ylim=c(0,2000), 
    main = 'Histogram of Sample Average for 1 Coin Flip', xlab= 'Sample Average')

This is just for a coin flip, so I have 6 possible values and want to have 6 buckets with the x-axis tick marks underneath each respective bar.

Any help is much appreciated.

like image 883
ShortMyCDS Avatar asked Nov 19 '12 20:11

ShortMyCDS


1 Answers

hist() returns the x coordinate of the midpoints of the bars in the mids components, so you can do this:

sample_avg <- sample(size=10000,x=seq(1,6),replace=TRUE)
foo <- hist(sample_avg, breaks =7, ylim=c(0,2000), 
    main = 'Histogram of Sample Average for 1 Coin Flip', xlab= 'Sample Average',
    xaxt="n")
axis(side=1,at=foo$mids,labels=seq(1,5))
like image 96
Stephan Kolassa Avatar answered Sep 22 '22 16:09

Stephan Kolassa