2017-07-31 19 views
0

我不知道如何将标签leftright一边放在facet_grid条的一侧。在facet_grid的条纹外面放置标签

作为一个重复的例子,我想先从这个例子

library(ggplot2) 
    ggplot(mtcars, aes("", hp)) + 
     geom_boxplot(width=0.7, position=position_dodge(0.7)) + 
     theme_bw() + 
     facet_grid(. ~ vs + am + carb,switch = 'both',labeller = label_both) + 
     theme(panel.spacing=unit(0.2,"lines"), 
      strip.background=element_rect(color="grey30", fill="grey90"), 
      panel.border=element_rect(color="grey90"), 
      axis.ticks.x=element_blank())+ 
      #strip.placement="outside") + 
     labs(x="") 

enter image description here

我找的情节;

enter image description here

+1

不要以为可以做到的 “直接”。试着在第二个图表中绘制图形,然后调用类似'grid.text(“vs”,x = 0.98,y = 0.1)',尽管你必须摆弄数字以准确地将它放在需要的地方。 –

+1

例如https://stackoverflow.com/a/29594608/2706826 –

回答

3

可以随时标题手动添加到gtable,

enter image description here

g <- ggplotGrob(p) 

library(gtable) 
library(grid) 
library(gridExtra) 
pos <- subset(g$layout, grepl("strip",name)) 
titles <- tableGrob(c("vs","am","carb"), theme = ttheme_minimal(9)) 
titles$heights <- unit(rep(1,3), "null") 
g$widths[ncol(g)] <- sum(titles$widths) 
g <- gtable_add_grob(g, titles, t=unique(pos$t), l = ncol(g)) 
grid.newpage() 
grid.draw(g) 
+0

感谢您的答案。其实我正在寻找这样的答案,因为我也试图将常见条合并为一个共同的条,如[这个问题](https://stackoverflow.com/questions/40732543/seeking-workaround-for-gtable-add -grob-code-broken-by-ggplot-2-2-0)。是可以将这些行添加到'OverlappingStripLabels'? – Alexander

+0

作为'pos'将有两个不同的定义。 – Alexander

+0

嗨@baptiste你能看到我最后的评论吗?我尝试了一些,但仍然无法将您的解决方案合并到'OverlappingStripLabels()'中 – Alexander

相关问题