# ggplot graphing of proportions of observations within categories

I am looking for advice on better ways to plot the proportion of observations in various categories.

I have a dataframe that looks something like this:

cat1 <- c("high", "low", "high", "high", "high", "low", "low", "low", "high", "low", "low") cat2 <- c("1-young", "3-old", "2-middle-aged", "3-old", "2-middle-aged", "2-middle-aged", "1-young", "1-young", "3-old", "3-old", "1-young") df <- as.data.frame(cbind(cat1, cat2))

In the example here, I want to plot the proportion *of each age group* that have the value "high", and the proportion *of each age group* that have the value "low". More generally, I want to plot, *for each value of category 2*, the percent of observations that fall into each of the levels of category 1.

The following code produces the right result, but only by manually counting and dividing before plotting. Is there a good way to do this on the fly within ggplot?

library(plyr) count1 <- count(df, vars=c("cat1", "cat2")) count2 <- count(df, "cat2") count1$totals <- count2$freq count1$pct <- count1$freq / count1$totals ggplot(data = count1, aes(x=cat2, y=pct))+ facet_wrap(~cat1)+ geom_bar()

This previous stackoverflow question offers something similar, with the following code:

ggplot(mydataf, aes(x = foo)) + geom_bar(aes(y = (..count..)/sum(..count..)))

But I do not want "sum(..count..)" - which gives the sum of the count of *all* the bins - in the denominator; rather, I want the sum of the count of each of the "cat2" categories. I have also studied the stat_bin documentation.

I would be grateful for any tips and suggestions on how to make this work.

## Answers

I will understand if this isn't really what you're looking for, but I found your description of what you wanted very confusing until I realized that you were simply trying to visualize your data in a way that seemed very unnatural to me.

If someone asked me to produce a graph with the proportions within each category, I'd probably turn to a segmented bar chart:

ggplot(df,aes(x = cat2,fill = cat1)) + geom_bar(position = "fill")

Note the y axis records proportions, not counts, as you wanted.

This might be a bit late for you and it is not involving ggplot, BUT:

I think mosaicplots are the way forward to visualise the interaction of two factors:

cat1 <- c("high", "low", "high", "high", "high", "low", "low", "low", "high", "low", "low") cat2 <- c("1-young", "3-old", "2-middle-aged", "3-old", "2-middle-aged", "2-middle-aged", "1-young", "1-young", "3-old", "3-old", "1-young") df <- as.data.frame(cbind(cat1, cat2)) mosaicplot(cat2 ~ cat1, data = df, col = c(lightskyblue2', 'tomato'))

In this plot, boxes for each value pair are scaled according o the number of observations in that category. You can provide a colour vector to aid with visualisation.