6

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)

This is how it looks so far

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.

Jaap
  • 81,064
  • 34
  • 182
  • 193
Martin
  • 307
  • 3
  • 10

1 Answers1

11

It's probably better to calculate the percentages beforehand:

library(dplyr)
dfl <- df %>% 
  group_by(group,choice) %>% 
  summarise(n=n()) %>% 
  group_by(group) %>% 
  mutate(perc=100*n/sum(n))

ggplot(dfl, aes(x=group, y=perc, fill=group)) +
  geom_bar(stat="identity") +
  ylab("percent") + 
  facet_wrap(~ choice)

this gives: enter image description here


Another (and probably better) way of presenting the data is to use facets by group:

ggplot(dfl, aes(x=choice, y=perc, fill=choice)) +
  geom_bar(stat="identity") +
  ylab("percent") + 
  facet_wrap(~ group)

this gives: enter image description here

Jaap
  • 81,064
  • 34
  • 182
  • 193