ggplot2每个facet_grid的单独ylabs

时间:2017-11-08 21:00:29

标签: r ggplot2 axis-labels facet-grid

我用Inkscape编辑了这个情节,不过我想知道在ggplot环境中是否有办法做到这一点?

enter image description here

我在网上快速搜索但没有任何成功。有人可以给我一个解决方法吗?

我真的很感激,因为我认为这是一种非常常见的情节。

该图的代码是:

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 = "")

使var3数据框看起来像:

# 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

1 个答案:

答案 0 :(得分:1)

您可以尝试此解决方法

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