关于在图中添加标签

时间:2013-07-28 22:48:34

标签: r

我的数据集如下:

  group  ratio
  0-0.2    58%
0.2-0.4    68%
0.4-0.6    60%
0.6-0.8    80%

我想在下图中可视化上面的数据集,

enter image description here

如何在R中实现这一点,尤其是在图中添加这些标签,例如68%?

2 个答案:

答案 0 :(得分:4)

更可重用的解决方案;

data<-runif(1000)
myLab<-diff(unlist(lapply(seq(0,1,0.2), function(y){sum(data<y)})))*100/length(data)
hist(data,breaks=seq(0,1,0.2), labels =paste0(myLab,"%"), col ="yellow")

enter image description here

答案 1 :(得分:3)

忽略重复非零y轴,这样的事情会起作用:

x <- c(58, 68, 60, 80)
savplot <- barplot(x,space=0,col="yellow",border=NA)
axis(1,at=0:length(x),labels=seq(0,0.8,0.2))
par(xpd=NA)
text(labels=paste0(x,"%"),savplot,x+5,cex=1.5)
par(xpd=TRUE)

enter image description here