Rhodo
Rhodo

Reputation: 1334

R histogram number of instances in each bin on plot

I would like to see the number of instances for each bin show up on the graph as well

set.seed(1)
x<-rnorm(1:100)
hist(x)

Upvotes: 3

Views: 1909

Answers (3)

Sanchit
Sanchit

Reputation: 3289

Another much simpler solution is just to use labels=TRUE in the hist(...) method itself. It will include number of occurrences/counts on the top of each bins in the histogram plot.

However, I would recommend to always include xlim and ylim for the histogram plots.

Code:

 set.seed(1)
 x <- rnorm(1:100)
 hist(x, xlim = c(-3,3), ylim = c(1,30), labels = TRUE) 

Upvotes: 2

Jilber Urbina
Jilber Urbina

Reputation: 61154

Try this

set.seed(1)
x<-rnorm(1:100)
y <- hist(x, plot=FALSE)
plot(y, ylim=c(0, max(y$counts)+5))
text(y$mids, y$counts+3, y$counts, cex=0.75)

which gives:

enter image description here

Upvotes: 5

AwokeKnowing
AwokeKnowing

Reputation: 8226

that automatically happen. it's called "frequency" on the left

Upvotes: 0

Related Questions