Suppose I have samples drawn from categories A, B, C. Within those categories, I have subcategories d,e,f which are found in all 3 categories. I want to visualize how many samples I have form categories A, B, C and the proportional composition of subcategories d,e,f within each category.

One way to do this is a bar plot (I'm using ggplot2, not that it matters too much) with bars for A, B, C, heights proportional to their total number of samples. Within each bar I partition it by fill color based on the composition of d,e,f within the category. The problem with this is that since A, B, and C will be different heights, it's almost impossible to visually compare the proportions - e.g. proportion of d in A with the proportion of d in B.

To see the proportions, I can renormalize the heights to 100% instead of the sample count so that bars for A, B, and C are now equal height. However, now I can't visualize the counts in A, B, and C.

Is there an elegant way to visualize both of these piece of information simultaneously?

  • 2
    $\begingroup$ Hadley Wickham's product plots seems to a fruitful approach to what you are suggesting, see this answer with an example and further reference. $\endgroup$
    – Andy W
    May 31, 2012 at 18:25
  • $\begingroup$ Thanks @Andy W ... a spine plot is actually perfect for this since the subcategories are the same across categories (I also tried mosaic plots, but a spine plot is better for comparisons). Using the horizontal width for the category counts seems so obvious in retrospect. Now if only I could do this in ggplot2 without those messy horizontal and vertical stacking calculations (there's a spineplot function in the base package, but it looks pretty ugly). $\endgroup$
    – user4733
    Jun 1, 2012 at 17:29

1 Answer 1


This example of embedded/layered bar plots may represent one alternative. The three main categories are represented by individual bars, then embedded within are subcategory bars (created in ggplot2).

Blog Link (Learning R)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.