Show element values in barplot

后端 未结 1 1585
情书的邮戳
情书的邮戳 2020-12-02 01:13

I\'m trying to put original counts into a barplot, but it\'s always messy:

set.seed(123)
c<-c(2, 3.5, 5, 7.9, 8.8, 12.3)
x<-sample(c, 100, replace=T)
b         


        
相关标签:
1条回答
  • 2020-12-02 01:46

    You just need to make sure that ylim is large enough such that none of the text will be placed outside of the plot area, and then you need the positions of the middle of bars and make sure to place the text there.

    set.seed(123)
    origin <-c(2, 3.5, 5, 7.9, 8.8, 12.3)
    x<-sample(origin, 100, replace=T)
    b<-barplot(table(x),ylim=c(0,22))
    text(x=b, y= table(x)+1, labels=as.character(table(x)))
    

    enter image description here

    0 讨论(0)
提交回复
热议问题