2014-09-03 84 views
0

enter image description here我正遇到问题以便将标签放在直方图垃圾箱的正确位置,如图中所示。 我使用这个代码:标签在直方图中不适合我的垃圾箱中心

![# Category names 
my.names <- c("test1", "test2", "test3", "test4", "test5", "test6") 

# Example data 
data <- c(191,33,12,254,22,74) 

# Normalize the example data as a percentage of the total 
data.norm <- data/sum(data) 

# Use barplot to plot the results 
barplot(data.norm, beside=TRUE, col=c("grey10","grey20","grey30","grey40","grey50","grey60")) 
text(1:6, par("usr")[3], labels=my.names, srt=45, pos=2, xpd=TRUE,offset=0.01)][2] 

请帮 问候

+0

酒吧不是在1:6绘制。您可以从barplot函数中导出实际的中点。看看[this](http://stackoverflow.com/a/25037416/2516066)的答案。 – koekenbakker 2014-09-03 15:38:59

+0

也许这是一个更好的重复,因为它直接指向'barplot'而不是'hist':http://stackoverflow.com/questions/18634431/r-in-barplot-midpoints-are-not-centered-wrt-bar 。我没有关闭自己,以防万一有人知道更好的重复。 – MrFlick 2014-09-03 15:42:29

回答

1

x轴上的值不一定是整数值。 barplot函数返回在绘图期间使用的x值。尝试

bp<-barplot(data.norm, beside=TRUE, col=c("grey10","grey20","grey30","grey40","grey50","grey60")) 
text(bp, par("usr")[3], labels=my.names, srt=45, pos=2, xpd=TRUE,offset=0.01) 

改为。

enter image description here