【问题标题】:Placing labels outside of the strips in facet_grid在 facet_grid 的条带之外放置标签
【发布时间】:2017-07-31 23:52:32
【问题描述】:

我想知道如何在facet_grid 条带的一侧放置标签leftright

作为一个可重现的例子,我想从这个例子开始

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

我要找的情节;

【问题讨论】:

  • 不要认为可以“直接”完成。尝试按照第二张图表中的方式进行绘图,然后调用类似grid.text("vs", x = 0.98 , y = 0.1) 的方法,但您必须调整数字才能将其准确地放在需要的位置。

标签: r ggplot2 facet-grid


【解决方案1】:

您始终可以手动将标题添加到 gtable,

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)

【讨论】:

  • 感谢您的回答。实际上,我一直在寻找这种答案,因为我也在尝试将常用条合并到一个公共条中,就像在这个 question 中一样。是否可以将这些行添加到 OverlappingStripLabels 中?
  • as pos 会有两个不同的定义。
  • 嗨@baptiste 你能看看我最后的cmets吗?我尝试了一些但仍然无法将您的解决方案合并到OverlappingStripLabels()
猜你喜欢
  • 1970-01-01
  • 2022-01-18
  • 1970-01-01
  • 2016-10-13
  • 1970-01-01
  • 2010-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多