在ggplot2中为频率图创建数据标签?

时间:2016-09-13 06:34:13

标签: r ggplot2

this问题中,可以使用ggplot创建频率数字。例如:

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

我现在如何获取图中每个条形的数据标签?

谢谢。

1 个答案:

答案 0 :(得分:2)

您可以添加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