2
votes

I'm producing a faceted plot like this:

enter image description here

This is accomplished with:

df = data.frame(val=rnorm(2005), class=c(rep(c('a','b'), 1000), rep('c', 5)))
qplot(val, ..count.., data=df, geom='density', fill=I('black')) +
  opts(strip.text.y = theme_text()) +
  # scale_y_continuous(breaks=seq(0, 999, by=50)) +
  facet_grid(class ~ ., scale = "free", space = "free")

I intentionally do want the facets to be differently sized (taking up only as much vertical space as necessary) but maintain the same scale.

Now, there are some problems with this plot:

  • I'd like to impose a min height on each facet so that you can at least see the right label text and recognize the display as an empty density plot - currently, if I didn't know how I generated this plot, I would never have guessed what that artifact at the bottom was.

  • While the facets share the same scale, the y-axes don't share the same grids/ticks. I can force them to share the same grid by uncommenting the commented line, but then I'm manually calculating this - is there some way to just use the coarsest grid among any of the density plots?

Thanks in advance for any answers.

1

1 Answers

5
votes

Edited

I don't think that this is possible using an elegant parameter of ggplot, but will be happy to be proved wrong.

However, here is a workaround that has the desired effect. The idea is to add an invisible layer to the plot with a single point that is high enough on the y-axis to force each panel to be a minimum size. In this case I have manually selected this value as 50, but you should be able to create an algorithm that is based on a percentage of maximum count or similar:

df = data.frame(val=rnorm(2005), class=c(rep(c('a','b'), 1000), rep('c', 5)))
dg <- data.frame(class=c("a","b","c"), value=rep(50,3))
qplot(val, ..count.., data=df, geom='density', fill=I('black')) +
    opts(strip.text.y = theme_text()) +
    facet_grid(class ~ ., scales="free", space="free") + 
    geom_blank(data=dg, aes(x=0, y=value)) 

Edit 2 Thanks to @Thierry for the tip to use geom_blank rather than geom_point with invisible points.

enter image description here