I've tried everywhere to find the answer to this question but I am still stuck, so here it is:
I have a data frame data_1
that contains data from an ongoing latent profile analysis. The variables of interest for this question are profiles
and gender
.
I would like to plot gender distribution by profile, but within each profile show what % of each gender we have compared to the entire sample of this gender. For example, if we have 10 women and 5 in Profile 1, I want the text on top of the women bar for Profile 1 to show 50%.
Right now I am using the following code but it is giving me the percentage for the entire population, while I just want the percentage compared to the total number of women.
ggplot(data = subset(data_1, !is.na(gender)),
aes(x = gender, fill = gender)) + geom_bar() +
facet_grid(cols=vars(profiles)) + theme_minimal() +
scale_fill_brewer(palette = 'Accent', name = "Gender",
labels = c("Non-binary", "Man", "Woman")) +
labs(x = "Gender", title = "Gender distribution per LPA profile") +
geom_text(aes(y = ((..count..)/sum(..count..)),
label = scales::percent((..count..)/sum(..count..))),
stat = "count", vjust = -28)
Thanks in advance for your help!
I tried multiple alternatives including creating the variable within the dataset using summarize
and mutate
but with no success unfortunately.