The facet_grid
command from the ggplot2
package enables easy generation of multiple plots by group. The gridded layout (in contrast to facet_wrap
), however, means that group combinations without data are displayed as empty panels. Unfortunately, this makes it impossible to distinguish between panels where no data is available and panels with only NA
or zero values. consider the following example:
set.seed(42)
site <- c("A","B","C","D","E") %>% sample(100, replace=T)
year1 <- c("2010","2012","2014") %>% sample(50, replace=T)
year2 <- c("2010","2011","2012","2013","2014") %>% sample(50, replace=T)
year <- c(year1,year2)
class <- c("1","2","3") %>% sample(100, replace=T)
value <- rnorm(100,10,3) %>% round(0)
ind <- which(value %in% sample(value, 5))
value[ind] <- NA
df <- data.frame(site,year,class,value)
library(ggplot2)
ggplot(df, aes(x=class, y=value)) +
geom_bar(stat="identity") +
facet_grid(site~year)
I would like to make the no data vs only NAs/zeros cases more easy to grasp. I understand that I could probably replace the NAs with zeros and add information to to the plot at those places. However, I think that it would be easier to read, if the panels without data were visually distinguished in some way, e.g. crossed or greyed out, or have "no data" written across them (see below). Any suggestions will be very welcome.