r barplot:将数字放在条形图上方

时间:2015-12-08 01:50:41

标签: r

我按照this教程使用了barplot。但是,我很难将数字放在栏杆上。我整天都没有运气。有人可以帮忙吗?

我已附上.zip数据,绘制脚本和我现在拥有的图表。基本上,条形中间的数字(黑色)应位于每个条形的顶部并具有相应的值。现在,我刚刚在所有酒吧中获得最大值(这是错误的)。提取ZIP并输入“Rscript plot-storage.R”。在解压缩的文件夹中执行脚本。

谢谢,

Nodir

1 个答案:

答案 0 :(得分:0)

我通过查看标记为重复的网址解决了问题。感谢MrFlick!

我跟着绘制了条形图:

[...]
data <- structure(list(vdb, vdc, vdd, vde), 
                  .Names = c("4K", "16K", "32K", "64K"), 
                  class = "data.frame", row.names = c(NA, 4L))
colours <- c("green", "orange", "blue", "red")
barObj <- barplot(as.matrix(data), main=title, ylab = "IOPS (log scale)", 
                  cex.lab = 1.2, cex.main = 1.4, 
                  beside=TRUE, col=colours, log="y")
legend("topright", c("B", "C", "D", "E"), cex=1.3, bty="n", fill=colours)

添加以下两行将数字放在条形图上。

[...]
data <- structure(list(vdb, vdc, vdd, vde), 
                  .Names = c("4K", "16K", "32K", "64K"), 
                  class = "data.frame", row.names = c(NA, 4L))
values = c(vdb, vdc, vdd, vde) # added line
colours <- c("green", "orange", "blue", "red")
barObj <- barplot(as.matrix(data), main=title, ylab = "IOPS (log scale)", 
                  cex.lab = 1.2, cex.main = 1.4, 
                  beside=TRUE, col=colours, log="y")
text(x=barObj, y=values, label=values, cex=0.9, pos=3, col="black", xpd=TRUE) # added line
legend("topright", c("B", "C", "D", "E"), cex=1.3, bty="n", fill=colours)