在R中的每个条形组的顶部添加条形差异值

时间:2017-03-16 18:34:42

标签: r ggplot2 bar-chart

我有一个组条形图,显示了两种算法的三种不同指标。 summary plot

如何在每个条形图的顶部添加这两种不同算法之间的增加/减少百分比?例如,在精确条的顶部,我们应该有-0.017,对于覆盖条,我们应该在顶部有+0.3867。

这是我的R代码:

als_precision<-0.27069
als_ndcg<-0.1523
als_coverage<-362/3233

als_reg_precision<-0.2659
als_reg_ndcg<-0.1520
als_reg_coverage<-502/3233


barplot(matrix(c(als_precision,als_reg_precision,als_ndcg,als_reg_ndcg,als_coverage,als_reg_coverage),nr=2), beside=T, 
        col=c("red","blue"), 
        names.arg=c("Precision@10","NDCG@10","coverage"))
legend("topright", c("ALS","ALS+reg"), pch=15, 
       col=c("red","blue"), 
       bty="n")

1 个答案:

答案 0 :(得分:2)

您可以使用text将文字添加到地块中。

编辑原件未使用百分比更改。

barplot(matrix(c(als_precision,als_reg_precision,als_ndcg,als_reg_ndcg,
        als_coverage,als_reg_coverage),nr=2), beside=T, 
        col=c("red","blue"), ylim=c(0,0.30),
        names.arg=c("Precision@10","NDCG@10","coverage"))
legend("topright", c("ALS","ALS+reg"), pch=15, 
       col=c("red","blue"), 
       bty="n")

text(2, max(als_precision, als_reg_precision)+0.01, 
    round((als_reg_precision - als_precision)/als_precision,3))

text(5, max(als_ndcg, als_reg_ndcg)+0.01, 
    round((als_reg_ndcg - als_ndcg)/als_ndcg,3))

text(8, max(als_coverage, als_reg_coverage)+0.01, 
    round((als_reg_coverage - als_coverage)/als_coverage,3))

Bar Chart