Include unused factor levels in facets with ggplot2
Is there a way to include a panel for unused levels of a factor used in faceting?
The reason I want to do this is because I'm arranging several separate plots in rows showing different measures, with each row having the same number of facet_grid
panels in columns. Each plot should line up by column.
But when the data for one of the rows is missing data for a particular facet level, the number of panels will be different and the columns do not line up. For example, notice the last row is missing the "Mathematics II" panel:
Example plot http://dl.dropbox.com/u/14792859/ggplot2%20facet%20levels.png
The only work-around I can th开发者_如何学Cink of is to include a dummy data-point for any missing facet levels, but I would love to hear that there is an easier/cleaner way.
As Ben Bolker mentioned, this appears to be a bug in ggplot2 caused by a bug in plyr. This is slated to be fixed in the upcoming plyr 1.5 release.
- ggplot2 mailing list thread
- plyr fix
In the meanwhile, I worked around the issue by creating a single plot and using facet_grid, with rows specifying the different measures to be plotted. The problem with this is that I can't specify a different scale_fill_manual for the two bottom panels, so I used stroke color for the middle panel instead -- not ideal but good enough for now.
Thanks Ben for figuring this out and starting the mailing list conversation.
精彩评论