HimanAB
HimanAB

Reputation: 2573

Add the bars difference values on the top of each bar groups in R

I have a group bar plot that shows three different metrics for two algorithms. summary plot

How can I add the percentage of increase/decrease between this two different algorithms on the top of each bar? For example, on the top of the precision bar, we should have -0.017 and for the coverage bar we should have +0.3867 on the top.

Here is my R code:

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")

Upvotes: 1

Views: 289

Answers (1)

G5W
G5W

Reputation: 37641

You can use text to add the text to the plot.

Edit The original did not use the percentage change.

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

Upvotes: 2

Related Questions