将ggplot中条带标签的位置从顶部更改为底部?

发布于 2024-09-09 19:47:54 字数 47 浏览 3 评论 0原文

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

I know this is not quite a data visualization issue, but the boss asked for it, so I need to figure out if it is possible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

昇り龍 2024-09-16 19:47:55

2016 年搜索者的答案。

从 ggplot2 2.0 开始,switch 参数 将为 facet_gridfacet_wrap 执行此操作:

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

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

从 ggplot2 2.2.0 开始,

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

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

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

ggplot(...) + ... + facet_wrap(facets, strip.position="right")

An answer for those searching in 2016.

As of ggplot2 2.0, the switch argument will do this for facet_grid or facet_wrap:

By default, the labels are displayed on the top and right of the plot. If "x", the top labels will be displayed to the bottom. If "y", the right-hand side labels will be displayed to the left. Can also be set to "both".

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

As of ggplot2 2.2.0,

Strips can now be freely positioned in facet_wrap() using the
strip.position argument (deprecates switch).

Current docs, are still at 2.1, but strip.position is documented on the dev docs.

By default, the labels are displayed on the top of the plot. Using strip.position it is possible to place the labels on either of the four sides by setting strip.position = c("top", "bottom", "left", "right")

ggplot(...) + ... + facet_wrap(facets, strip.position="right")
oО清风挽发oО 2024-09-16 19:47:55

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

screenshot of graph

如果您愿意要获得下面的标签,您必须这样做

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

you can now use facet_wrap(~var, strip.position = "bottom"), though for some reason this results in the labels being located above the axis tick mark labels, rather than below (which I think would make more sense), as you can see from my screenshot of a small portion of my graph

screenshot of graph

If you want to have the label below, you have do to this

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

As seen here: https://github.com/tidyverse/ggplot2/issues/2622

云淡风轻 2024-09-16 19:47:55

作为 2024 年 6 月 ggplot2_3.5.1 的更新,switch 选项仍然发生变化:

ggplot(...) +
  ...
  facet_grid(rows = vars(facet_col_name), switch = "y") +
  theme(strip.placement = "outside")

此外,facet_grid() 函数使用 rows = vars()cols = vars() 而不是旧的公式表示法。

As an update for June 2024 with ggplot2_3.5.1, the switch option is still changed:

ggplot(...) +
  ...
  facet_grid(rows = vars(facet_col_name), switch = "y") +
  theme(strip.placement = "outside")

Also, the facet_grid() function uses rows = vars() and cols = vars() rather than the old formula notation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文