希望在R中将x轴的值绘制在条形图的中心。
目前遇到了无法实现这一点的问题,以下是代码:
hist(sample_avg, breaks =7, ylim=c(0,2000),
main = 'Histogram of Sample Average for 1 Coin Flip', xlab= 'Sample Average')
这只是一个抛硬币的例子,我有6个可能的值,并且想要有6个桶,每个柱形图下面都有相应的x轴刻度标记。
非常感谢任何帮助。
希望在R中将x轴的值绘制在条形图的中心。
目前遇到了无法实现这一点的问题,以下是代码:
hist(sample_avg, breaks =7, ylim=c(0,2000),
main = 'Histogram of Sample Average for 1 Coin Flip', xlab= 'Sample Average')
这只是一个抛硬币的例子,我有6个可能的值,并且想要有6个桶,每个柱形图下面都有相应的x轴刻度标记。
非常感谢任何帮助。
hist()
返回柱形图中间点的x坐标,存储在mids
组件中,因此您可以这样做:
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))
# 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))
可能不如您所期望的那么巧妙,但看起来最好的方法是使用axes=F,然后使用“axis”命令放入自己的坐标轴,指定您想要看到的刻度标记。
参考资料:https://stat.ethz.ch/pipermail/r-help/2008-June/164271.html
breaks=seq(0.5,6.5)
而不是 breaks=7
,这样可能会得到更好的结果。 - Stephan Kolassa
barplot
。默认的条形图还会将x轴标签居中放置在条形下方。 - BenBarnes