Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw a ggplot2 with facet_wrap, showing percentages from each group, not overall percentages?

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.

like image 748
Martin Avatar asked May 14 '15 09:05

Martin


People also ask

What is faceting in ggplot2?

Faceting is a great data visualization technique that uses “small multiples” i.e. the use of same type of plots multiple times in a panel. Each “small multiple” is a same type of plot but for a different group or category in the data. ggplot2 makes it really easy to make such “small multiples” with faceting.

How to use facet_wrap and create small multiple charts with ggplot2?

To use facet_wrap and create small multiple charts, you first need to be able to create basic data visualizations with ggplot. That means that you should first have a good understanding of the ggplot2 syntax.

What is a small multiple plot in ggplot2?

Each “small multiple” is a same type of plot but for a different group or category in the data. ggplot2 makes it really easy to make such “small multiples” with faceting. One of the functions in ggplot2 to make facetted plots is facet_wrap ().

What are the different parts of a ggplot2 data visualization?

There are 4 basic parts of a simple data visualization in ggplot2: the ggplot () function, the data parameter, the aes () function, and the geom specification. Let’s quickly talk about each part. The ggplot () function is the core function of the ggplot2 data visualization system.


Video Answer


1 Answers

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

like image 154
Jaap Avatar answered Oct 11 '22 10:10

Jaap