Это решение, использующее ..count..
. Все, что вам нужно сделать, это попросить geom_label
вычислить его.
library(dplyr)
library(ggplot2)
transfusion %>%
mutate(Group = ifelse(whether.he.she.donated.blood.in.March.2007 == 0, "Didn't donate", "Donated")) %>%
ggplot(aes(x = Group, fill = Group)) +
geom_bar(position = "dodge") +
geom_label(stat = 'count', aes(label = ..count..),
vjust = -0.1,
show.legend = FALSE) +
scale_y_continuous(breaks = seq(0, 500, by = 100)) +
ylab("Count of people") +
ggtitle("People who donated blood in march 2007") +
theme(plot.margin = margin(2, 0.8, 2, 0.8))
Данные
set.seed(1234)
transfusion <- data.frame(
whether.he.she.donated.blood.in.March.2007 = rbinom(800, 1, prob = c(0.3, 0.7))
)