Juanchi
Juanchi

Reputation: 1166

ggplot2 individual ylabs for each facet_grid

I edited this plot with Inkscape, however I wonder if is there a way to do it within ggplot environment?

enter image description here

I did a quick search in the web without any success. Can someone give me a workaround?

I would really appreciate because I consider it a very common type of plot.

The code for this plot is:

var3 %>% 
  gather(-(exp:rep), key = "variable", value ="value") %>%
  mutate(variable = recode(variable, 
                           diam = "Lesion diameter",
                           dles = "Lesion density",
                           sev = "Disease severity")) %>% 
  ggplot(aes(x = Iso, y = value)) + 
  geom_boxplot(aes(fill=factor(exp)),
               position = position_dodge(width = 0), alpha = 0.5, 
               width =0.3) +
  facet_grid(variable~Cv, 
             labeller = labeller(Cv = label_both, variable = label_value),
             scales = "free")+
  scale_y_continuous(labels=scaleFUN)+
  theme_juan(9, "top", "center")+
  scale_fill_manual(values = c("grey70","grey30"),
                    labels = c("Exp. 1","Exp. 2"), name = "")

Being var3a data frame looking like:

# A tibble: 72 x 7
     exp           Cv    Iso   rep        sev      dles      diam
   <dbl>       <fctr> <fctr> <dbl>      <dbl>     <dbl>     <dbl>
 1     1 BMX_Potencia     MT     1 0.17262317 1.8924050 0.2400000
 2     1 BMX_Potencia     MT     2 0.14700000 2.1483445 0.3000000
 3     1 BMX_Potencia     MT     3 0.01404289 1.3623602 0.2133333
 4     1 BMX_Potencia     MT     4 0.11933333 1.3160049 0.2900000

Upvotes: 3

Views: 1299

Answers (1)

baptiste
baptiste

Reputation: 77116

you could try this workaround

library(ggplot2)
library(grid)

d <- data.frame(x=1:4, f1=gl(2,2, labels=c("A","B")), f2=rep(letters[1:2],each=2))

p1 <- ggplot(d, aes(x,x)) + geom_blank() + 
  facet_grid(f1~f2)

p2 <- p1 + facet_grid(f1~f2, switch = 'y', 
                      labeller = labeller(f1=c(A="this", B="that")))+ 
  theme(strip.placement.y = "outside",
        strip.text.y = element_text(angle = 90),
        strip.background = element_blank()) 

g1 <- ggplotGrob(p1)
g2 <- ggplotGrob(p2)

grid.newpage()
grid.draw(gridExtra::gtable_cbind(g2[,-ncol(g2)], g1[,ncol(g1)-3]))

enter image description here

Upvotes: 2

Related Questions