Here is some example data:
gender <- c("male", "female", "male", "male", "female", "female", "male", "female", "female", "male")
outcome <- factor(c(0,0,0,1,1,1,0,1,1,1), levels = c(0,1), labels = c("responders", "non-responders"))
df <- c(gender, outcome)
I wish to create a ggplot where on the y axis is the percentage, x axis is the gender and the fill is the outcome. It has to be a stacked bar with percentage labels within.
Tried this code here:
ggplot (df, aes (x = gender, fill = outcome)) + geom_bar()
But this gives me the count in the y-axis. I wish to create the percentage on the y-axis. The stacked female bar must indicate the percentage of females with the "responder and non-responder outcome within the female group" as opposed to the percentage of females of the total population that respond or do not respond. E.g., I would like to see 40% female responders vs 60% non-responders and similar for males.
To make this ready for publication I also need to add labels of these percentages in the stacked bar.