99

我知道这不是一个数据可视化的问题,但老板要求它,所以我需要弄清楚是否可能。

谢谢!

4

4 回答 4

112

为那些在 2016 年寻找答案的人提供答案。

ggplot22.0 开始,switch 参数将为facet_gridor执行此操作facet_wrap

默认情况下,标签显示在图的顶部和右侧。如果为“x”,则顶部标签将显示在底部。如果为“y”,则右侧标签将显示在左侧。也可以设置为“两者”。

ggplot(...) + ... + facet_grid(facets, switch="both")

从 ggplot2 2.2.0 开始

条带现在可以facet_wrap()使用 strip.position 参数自由定位(不推荐使用switch)。

strip.position当前文档仍为 2.1,但记录在开发文档中。

默认情况下,标签显示在图的顶部。使用 strip.position 可以通过设置将标签放置在四个侧面中的任何一个上strip.position = c("top", "bottom", "left", "right")

ggplot(...) + ... + facet_wrap(facets, strip.position="right")
于 2016-12-01T17:47:21.427 回答
21

您现在可以使用facet_wrap(~var, strip.position = "bottom"),但由于某种原因,这会导致标签位于轴刻度标签上方,而不是下方(我认为这更有意义),正如您从我的图表一小部分的屏幕截图中看到的那样

图表截图

如果你想有下面的标签,你必须这样做

ggplot(zzz, aes(x = c1, y = c2)) +
  facet_wrap(~ gp, scales = "free", nrow = 3, strip.position = "bottom") +
  geom_point() +
  theme(
    aspect.ratio = 1,
    strip.background = element_blank(),
    strip.placement = "outside"
  )

如此处所示:https ://github.com/tidyverse/ggplot2/issues/2622

于 2019-09-24T15:20:45.790 回答
-3

答案是肯定的!

theme(strip.text=element_text(vjust=-10))

数字 -10 取决于您在绘图中使用的比例和单位。

于 2015-05-05T17:40:21.943 回答
-5

继续使用主题:

主题(strip.text=element_text(vjust=-10))

确保您的标签都有相同数量的回车。

Label /n Facet /n One将以不同于 的速度变化Label /n Facet Two

于 2015-10-22T13:26:25.137 回答