0
votes

How do you create a faceted plot using ggplot2 where each facet is plotted if and only if the number of observations in the facet exceeds some specified threshold? This question explores how to annotate each facet with the number of observations in the facet, but I'd like to be able to specify a threshold where every facet that has fewer observations than the threshold is not plotted at all.

Here is a straightforward example of the type of plot that I'd like to specify the threshold for:

require(ggplot2)
p <- ggplot(data,aes(x=xaxis)) + geom_density()
p <- p + facet_grid(ab1 ~ ab2)

This produces a faceted plot where some facets have too few observations to be meaningful, so I'd like an empty grid/space there instead of plotting misleading data.

1
Subset your data so it only includes data for the facets you want plotted. If you need help with an example, provide reproducible data in your question.Gregor Thomas

1 Answers

4
votes

Since you want to keep empty facets when there's not enough data (at least that's what I took your last sentence to mean), you can replace data values with NA for groups that are too small.

Here's an example, using the built-in mtcars data frame. We use dplyr's chaining operator (%>%) to group by the carb column and to do the NA replacement on the fly for all groups with fewer than 8 rows of data:

  library(ggplot2)
  library(dplyr)

  ggplot(mtcars %>% group_by(carb) %>%
           mutate(mpg = if(n() >= 8) mpg else NA_real_),
         aes(mpg)) + 
    geom_density() +
    facet_grid(. ~ carb)

enter image description here

If you want to plot only those facets with at least 8 observations, you could do this:

  ggplot(mtcars %>% group_by(carb) %>%
           filter(n() >= 8),
         aes(mpg)) + 
    geom_density() +
    facet_grid(. ~ carb)