3
votes

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 think 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.

1
Are you using the same dataframe for each of the plots, or are you creating new ones for each? - James
facet_wrap(..., drop=FALSE) is supposed to work but doesn't seem to, in my version of ggplot2 -- have sent a query to the ggplot2 list - Ben Bolker
@James It's a different data.frame for each plot, but I make sure the variable for faceting is a factor with the same levels (e.g. factor(df$subject, levels=union(levels(df$subject, other.df$subject))). - Daniel Dickison
@Ben I hadn't noticed that facet_wrap has a drop argument. I tried it now, and like you said it doesn't seem to work. - Daniel Dickison

1 Answers

4
votes

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.

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.