'Is it possible to skip "empty" values/categories in facets in ggplot2?

Let's say I have this kind of data:

set.seed(42)
type<-c("a","b","c","a","a","a","a","b","b","c","a","b")
code<-as.factor(c(rep(1:3,4)))
result<-(runif(12)*10)

data<-data.frame(type, code, result)

library(ggplot2)

ggplot(data, aes(x = code, y = result, fill = code, shape = code )) +
         geom_boxplot(alpha = 0.5)+
         geom_point(position=position_dodge(0.76), size = 3)+
         facet_wrap(~type, nrow = 1)

Which results in this graph created by ggplot2, where data is facetted by "type" column: graph facetted by "type" column

There are "empty" categories in facets "b" (no values for "1") and "c" (no values for "2"). I'm wondering whether it's possible somehow to skip this missing values/categories in those facets and show only the categories that have values for this specific facet ? Meaning, "a" would contain all three boxplots as have data for all of them, "b" would contain two boxplots for "2" and "3", no code "1" parameter in this facet as it's empty, "c" would contain boxplot for "1" and "3", no code "2" parameter in this facet.

Thank you!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source