Я хочу установить количество наблюдений в верхней части каждого бара.
Это пример данных
structure(list(Treatment = structure(c(3L, 3L, 3L, 3L, 3L, 4L,
4L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L), .Label = c("", "{\"ImportId\":\"Treatment\"}",
"Altruism", "Altruism - White", "Piece Rate - 0 cents", "Piece Rate - 3 cents",
"Piece Rate - 6 cents", "Piece Rate - 9 cents", "Reciprocity",
"Reciprocity - Black", "Reciprocity - White", "Treatment"), class = "factor"),
Gender = structure(c(5L, 3L, 5L, 5L, 5L, 3L, 3L, 7L, 3L,
3L, 5L, 5L, 3L, 3L, 5L), .Label = c("", "{\"ImportId\":\"QID2\"}",
"Female", "Gender you most closely identify with: - Selected Choice",
"Male", "Other", "Prefer not to answer"), class = "factor")), row.names = c(NA,
15L), class = "data.frame")
Мой подход заключался в использовании следующего кода
totals <- Data1 %>%
group_by(Gender) %>%
summarize(total = n)
Data1 %>%
count(Treatment, Gender) %>%
ggplot(aes(Treatment, n))+
geom_col(aes(fill = Gender), position = "fill")+
ggtitle("Gender")+
ylab("Fraction")+
theme(axis.text.x = element_text(angle = 90, vjust=0.3, hjust=1))+
scale_fill_manual("Gender",
values = c("Female" = "pink", "Male" = "light blue",
"Other"="coral", "Prefer not to answer"="violet"))+
geom_text(aes(label=n, group=Gender),size=3,
position = position_fill(vjust=0.5),data<-totals)
Я хочу, чтобы общее количество наблюдений отображалось в верхней части каждого столбца.
Мой график пока выглядит так

Теперь я хочу знать, как отображать общее количество наблюдений для каждого столбца.