user2716568
user2716568

Reputation: 1946

Create data labels for frequency plot in ggplot2?

In this question, a frequency figure can be created using ggplot. For example:

f <- factor(sample(letters[1:5], 100, r=T))
h <- table(f) / length(f)
dat <- data.frame(f = f)
ggplot(dat, aes(x=f, y=..count.. / sum(..count..), fill=f)) + geom_bar()

How do I now obtain data labels for each individual bar within the figure?

Thank you.

Upvotes: 0

Views: 986

Answers (1)

mtoto
mtoto

Reputation: 24188

You can add geom_text():

library(ggplot2)
ggplot(dat, aes(x=f, y=..count.. / sum(..count..), fill=f)) + 
  geom_bar() +
  geom_text(stat = "count", aes(label=..count../100))

enter image description here

Upvotes: 2

Related Questions