In this example graph, the count of each group (or 'type') appears three times below the percentage of each result. What I am trying to do is to show the count ('total') just once per type, and to place it to the left, below the y-label, underneath A, B, and C. Alternatively, the count could be shown to the right of the bar. Do you have any suggestions on how I can achieve this?
df <- data.frame(
type = c("A", "A","A", "B", "B","B", "C", "C","C"),
result = c("negative", "positive", "negative", "unknow", "positive", "positive",
"negative","positive", "unknow" ))
myplot_a <- df %>%
count(type, result) %>%
group_by(type) %>%
mutate(pct= prop.table(n) * 100,
total = sum(n)) %>% # add a column for total count
ggplot() + aes(y= type, pct, fill=result) +
geom_bar(stat="identity") +
ylab("Type") +
labs(fill = "", x = "Percentage") +
scale_fill_manual(values=c("negative"="#00b3b3", "unknow"="#4d4d4d", "positive"="#e60000")) +
geom_text(aes(label=paste0(sprintf("%1.1f", pct),"%\n", total)),
position=position_stack(vjust=0.5)) +
theme_bw()+
theme(text = element_text(size = 15)) #font size
myplot_a