I would like to add percentage labels to stacked barplot using ggplo2. Here is my code. It does not work so far.
df <- longer_data %>%
drop_na(response) %>%
group_by(question) %>%
count(response) %>%
mutate(prop = percent(response / sum(response))) %>%
mutate(response = factor(response, levels = 1:3, labels = c("Yes", "No", "I don't know"))) %>%
mutate(prop = percent(response / sum(response))) %>%
ggplot(df, aes(x = question, fill = response)) +
geom_bar(stat= "count", position = "fill") +
labs(title =" Please indicate which part of the driving task shown on the interface are \n performed by the car or you, the driver of the car.", subtitle =" Speed and distance control" )+scale_fill_manual(values = c("Yes" = "Forestgreen", "No" = "Darkred", "I don't know" = "Grey")) +labs(x ="HMIs", y = "Percentage") +scale_y_continuous(labels = scales::percent) +theme(axis.text.x = element_text(angle = 360, hjust = 0))
Here is a snippet of my data:
structure(list(question = c("HMI1", "HMI2", "HMI3", "HMI4", "HMI5",
"HMI6", "HMI1", "HMI2", "HMI3", "HMI4"), response = c("1", "1",
"1", "1", "1", "1", "1", "1", "1", "3")),
row.names = c(NA, -10L ), class = c("tbl_df", "tbl", "data.frame"))