nouse
nouse

Reputation: 3471

Individual axes labels in facet_wrap without scales="free"

My data looks like this:

df <- data.frame(Year = as.factor(c(rep(2015, 3), rep(2016, 3), rep(2017,3))),
                 Tax = as.factor(c(rep(c("A", "B", "C"), 3))),
                 Depth = as.factor(c(10, 30, 50, 20,30,50,10,30,40)),
                 values= c(0.5, 0.25, 0.25, 0.1, 0.4, 0.5, 0.2, 0.6, 0.2))

I want to plot it with gaps for missing data and individual axis labels.

library(ggplot2)     

The scale argument of facet_wrap gives individual axes, but is not performing as desired, as missing data is not reflected.:

ggplot(df, aes(Depth, values, fill=Tax)) + geom_bar(stat="identity")+
  facet_wrap(~Year, scale="free") +
  coord_flip()

Without scales:

ggplot(df, aes(Depth, values, fill=Tax)) + geom_bar(stat="identity")+
  facet_wrap(~Year) +
  coord_flip()

The missing data is represented (which i want!), but it lacks axis labels (which i need).

is there anything i can do?

Upvotes: 0

Views: 143

Answers (1)

jludewig
jludewig

Reputation: 428

It looks like this can be done using the lemon package:

library(tidyverse)
library(lemon)

df <- data.frame(Year = as.factor(c(rep(2015, 3), rep(2016, 3), rep(2017,3))),
                 Tax = as.factor(c(rep(c("A", "B", "C"), 3))),
                 Depth = as.factor(c(10, 30, 50, 20,30,50,10,30,40)),
                 values= c(0.5, 0.25, 0.25, 0.1, 0.4, 0.5, 0.2, 0.6, 0.2))

ggplot(df, aes(Depth, values, fill=Tax)) + geom_bar(stat="identity")+
   facet_rep_wrap(~Year,repeat.tick.labels = T) +
   coord_flip()

Upvotes: 1

Related Questions