Skip to content

Loading…

Subset in geom leaves empty panels in grid #758

Closed
joshbode opened this Issue · 5 comments

2 participants

@joshbode

Hi,

I'm playing with the subset argument of geom_* and I'm finding that I end up with empty panels in the grid. I'm using version 0.9.3 of ggplot2.

Here is my toy-example:

d = data.frame(
  x=ceiling(seq(1, 30) / 3), y=runif(30),
  group=as.factor(rep(c('A', 'B', 'C'), 10))
)

ggplot(d, aes(x=x, y=y, group=group, color=group)) +
geom_point(subset=.(group != 'B')) +
facet_grid(group ~ ., drop=TRUE)

Note: It makes no difference if group is a factor or not.

image

The legend correctly omits B, but the empty panel for B is still present.

The obvious workaround is to subset the data upfront, e.g.

ggplot(subset(d, group != 'B'), aes(x=x, y=y, group=group, color=group)) +
geom_point() +
facet_grid(group ~ .)

Note: If I set drop=FALSE in facet_grid in the workaround example, then I end up with the same result as above.

However, it seems to me like the subset argument of geom_* could potentially take care of this. If this is not a bug, I would like to make it a feature request (and if I get my head around the code-base, I might take a stab :)

Cheers,
Josh

@joshbode

I'm not sure how one should reference this properly in github, but this commit to my fork (with the few preceding it) is an initial attempt at addressing this issue (as I see it):

joshbode/ggplot2@d2a66d3

@hadley
Owner

I'm not convinced that this is a bug - to me it seems like subsetting at the geom level should affect the display of the individual geoms, but not the panels. It would help if you could provide more reasons why this behaviour is confusing or causing problems - as you've pointed out, there's already a work around for it, and the code that deals with subsetting is already quite fragile :/

@joshbode

The reason I was thinking about this was because I was trying to set up a basic plot (call it p) (with data, headings, etc. but with no geoms) that I could then repeatedly re-use by adding on subsetted geoms and produce individual faceted plots that way, e.g.

p = ggplot(d, aes(x=x, y=y, group=group, color=group)) +
facet_grid(group ~ .)

p + geom_point(subset=.(group != 'B'))

p + geom_point(subset=.(group != 'A'))

rather than have to redefine p for each individual plot, however, as I discussed, this leads to plots with empty panels. My toy example didn't have many groups, but for what I intended to use this, I actually have many groups of sub-groups that I would like to display this way, reusing a common base p.

Also, it seemed to me to be inconsistent behaviour that the drop argument of facet_grid actually would remove the empty panels (due to being passed subsetted data frame with empty levels at the outset), however the subset argument of the geom_* elements still produced empty panels, even though there was no geom_* actively requesting them.

@joshbode

By the way, devtools made it so easy for me to muck around with this ;) So, uh, thanks for that!

@hadley
Owner

This sounds like a great feature, but unfortunately we don't currently have the development bandwidth to support it. If you'd like to submit a pull request that implements this feature, please follow the instructions in the development vignette.

@hadley hadley closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.