R中直方图中条形图的居中值

Sho*_*CDS 8 r

期望将x轴的值绘制在R中的条的中心.

遇到问题找到使这成为可能的方法,代码如下:

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

这只是一个硬币翻转,所以我有6个可能的值,并希望有6个桶,每个相应的条下面有x轴刻度线.

任何帮助深表感谢.

S. *_*ica 12

hist()返回mids组件中条形中点的x坐标,因此您可以这样做:

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))
Run Code Online (Sandbox Code Playgroud)


Joh*_*ell 0

不像您希望的那么漂亮,但看起来最好的办法是使用axes=F,然后使用“axis”命令放入您自己的轴,指定您想要查看的刻度线。

参考:https ://stat.ethz.ch/pipermail/r-help/2008-June/164271.html