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

时间:2012-11-19 20:40:52

标签: r

希望将x轴的值绘制在R中的条形中心。

在找到使这种方法成为可能的方法时,代码如下:

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

这仅适用于硬币翻转,因此我有6个可能的值,并希望在每个相应的条形下方有6个带有x轴刻度标记的桶。

非常感谢任何帮助。

3 个答案:

答案 0 :(得分: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))

答案 1 :(得分:2)

# when dealing with histogram of integers, 
# then adding some residual ~ 0.001 will fix it all...
# example:
v = c(-3,5,5,4,10,8,8)
a = min(v)
b = max(v)
foo = hist(v+0.001,breaks=b-a,xaxt="n",col="orange",
panel.first=grid(),main="Histogram of v",xlab="v")
axis(side=1,at=foo$mids,labels=seq(a,b))

答案 2 :(得分:0)

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

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

相关问题