Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show element values in barplot

Tags:

r

bar-chart

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)
barplot(table(x))
text(c, table(x)+2, labels=as.character(table(x)))

enter image description here

Would somebody have some suggestions?

like image 938
David Z Avatar asked Mar 18 '26 15:03

David Z


1 Answers

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

like image 178
keegan Avatar answered Mar 21 '26 05:03

keegan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!