I'd like to draw a ggplot with facet_wrap, which doesn't show the actual table percent, but the percent of the given answer in each group. I have to do this, because I want to show, which answer is most selected and most important for each group. The groups don't have the same size.
Example Data:
group <- c(rep(c("Group1"), times = 10),rep(c("Group2"), times = 6),rep(c("Group3"), times = 4))
choice <- c(rep(c("a","b","c"),length.out = 10), "a","a","a","a","b","c","b","b","b","c")
df <- data.frame(cbind(group,choice))
It would be nice, if I could not use the overall prop.t
, but prop.c
to show in my plot, because it is important to show, for example that 66.67% of group 2 prefers choice a.
library(gmodels)
CrossTable(choice, group, prop.chisq=FALSE, prop.t = TRUE, prop.c = TRUE, prop.r = FALSE, format = "SPSS")
This is for the plot:
library(ggplot2)
g <- ggplot(df, aes_string(x="group", fill="group")) +
geom_bar(aes(y = (..count..)/sum(..count..)))+
ylab("percent")
g + facet_wrap(~ choice)
Now the first bar show: 20%, 20%, 0%, but should show 40%, 66.67% and 0% (the percent of each person in the group, who gave this answer).
For the second bar should show: 30%, 16.667% and 75%.
and the third bar: 30%, 16.667% and 25%
Thank you for your help.