0

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.

enter image description here

M.Teich
  • 575
  • 5
  • 22
  • how are NA different than "missing data" in your case? Your sample data frame doesn't contain zeros and might therefore not be the best example – tjebo Jul 04 '19 at 12:41
  • @Tjebo Maybe I didn't clarify that enough in my example. This is fisheries data where not all sites were fished in all years, i.e. missing data, while other sites were fished, but did not yield any catch, i.e. NAs – M.Teich Jul 04 '19 at 12:46
  • 1
    I would suggest to use `NA` for actually "missing data" and "0" for no catch. – tjebo Jul 04 '19 at 12:48
  • 1
    Then, have a look at https://stackoverflow.com/questions/30372368/adding-empty-graphs-to-facet-wrap-in-ggplot2 – tjebo Jul 04 '19 at 12:51
  • @Tjebo Good point, but then I have to find years without data and manually add `NA`, which renders the convenience of `facet_grid` obsolete and still leaves me with the issue of adding this information to the relevant panels. – M.Teich Jul 04 '19 at 12:52
  • @Tjebo Hadn't seen that post. That may be worth a try... – M.Teich Jul 04 '19 at 12:55
  • 1
    `tidyr::complete` might help filling years without data – Richard Telford Jul 04 '19 at 20:08

0 Answers0