保留 ggplot 中 facet_grid 后的 Scale_x_discrete 刻度标签

huangapple go评论112阅读模式
英文:

Keeping the Scale_x_discrete tick labels in ggplot after facet_grid

问题

我试图在facet_grid之后添加一个新的x轴刻度标签,但似乎不像我希望的那样工作。希望这里有人经历过类似情况并知道解决方法。

为了使代码可复制,我正在使用mpg数据集,希望每个人都能理解问题。

使用下面的代码,我正在将刻度轴标签更改为每个类别的汽车总数:

  1. library(tidyverse)
  2. library(ggpubr)
  3. p3 <- mpg %>%
  4. filter(class == c("compact", "subcompact")) %>%
  5. filter(displ == c(2, 2.5)) %>%
  6. ggplot(aes(x = class, y = hwy, col = class)) +
  7. geom_violin() +
  8. geom_point() +
  9. #facet_grid(cols = vars(displ))+
  10. stat_compare_means(method = "t.test", comparisons = list(c("compact", "subcompact")), label = after_stat("p.signf")) + # Add statistical comparisons
  11. theme_bw() +
  12. scale_x_discrete(labels = function(x) {
  13. num_cars <- mpg %>%
  14. filter(class == c("compact", "subcompact")) %>%
  15. filter(displ == c(2, 2.5)) %>%
  16. filter(class %in% x) %>%
  17. group_by(class) %>%
  18. summarise(total_cars = n()) %>%
  19. mutate(label = total_cars)
  20. num_cars$label
  21. })
  22. p3

这将创建以下图表:

保留 ggplot 中 facet_grid 后的 Scale_x_discrete 刻度标签

当我将图表分开以便按displ分组时,x轴不再起作用,尽管统计数据仍然有效。

  1. p3 <- mpg %>%
  2. filter(class == c("compact", "subcompact")) %>%
  3. filter(displ == c(2, 2.5)) %>%
  4. ggplot(aes(x = class, y = hwy, col = class)) +
  5. geom_violin() +
  6. geom_point() +
  7. facet_grid(cols = vars(displ)) +
  8. stat_compare_means(method = "t.test", comparisons = list(c("compact", "subcompact")), label = after_stat("p.signf")) + # Add statistical comparisons
  9. theme_bw() +
  10. scale_x_discrete(labels = function(x) {
  11. num_cars <- mpg %>%
  12. filter(class == c("compact", "subcompact")) %>%
  13. filter(displ == c(2, 2.5)) %>%
  14. filter(class %in% x) %>%
  15. group_by(class) %>%
  16. summarise(total_cars = n()) %>%
  17. mutate(label = total_cars)
  18. num_cars$label
  19. })
  20. p3

结果如下:

保留 ggplot 中 facet_grid 后的 Scale_x_discrete 刻度标签

但我希望刻度标签为2、3、2、2。

希望有人能对此有一个简单的答案(我确信这很简单)。

干杯!

英文:

I am trying to add a new x tick label after facet_grid and it does not seem to work as I wished. Hopefully someone here has gone through the same and knows the trick.

To make the code reproducible I am using the mpg dataset and hopefully everyone can understand the question.

With the code below I am changing the tick axis label to the total number of cars per category:

  1. library(tidyverse)
  2. library(ggpubr)
  3. p3 &lt;- mpg %&gt;%
  4. filter(class == c(&quot;compact&quot;, &quot;subcompact&quot;)) %&gt;%
  5. filter(displ == c(2,2.5)) %&gt;%
  6. ggplot(aes(x=class, y=hwy, col=class))+
  7. geom_violin()+
  8. geom_point()+
  9. #facet_grid(cols = vars(displ))+
  10. stat_compare_means(method = &quot;t.test&quot;, comparisons = list(c(&quot;compact&quot;, &quot;subcompact&quot;)), label = after_stat(&quot;p.signf&quot;)) + # Add statistical comparisons
  11. theme_bw()+
  12. scale_x_discrete(labels = function(x) {
  13. num_cars &lt;- mpg %&gt;%
  14. filter(class == c(&quot;compact&quot;, &quot;subcompact&quot;)) %&gt;%
  15. filter(displ == c(2,2.5)) %&gt;%
  16. filter(class %in% x) %&gt;%
  17. group_by(class) %&gt;%
  18. summarise(total_cars = n())%&gt;%
  19. mutate(label = total_cars)
  20. num_cars$label
  21. })
  22. p3

This creates the following graph:

保留 ggplot 中 facet_grid 后的 Scale_x_discrete 刻度标签

When I facet the graph to separate it by displ, the x axis do not work anymore although the stats are working.

  1. p3 &lt;- mpg %&gt;%
  2. filter(class == c(&quot;compact&quot;, &quot;subcompact&quot;)) %&gt;%
  3. filter(displ == c(2,2.5)) %&gt;%
  4. ggplot(aes(x=class, y=hwy, col=class))+
  5. geom_violin()+
  6. geom_point()+
  7. facet_grid(cols = vars(displ))+
  8. stat_compare_means(method = &quot;t.test&quot;, comparisons = list(c(&quot;compact&quot;, &quot;subcompact&quot;)), label = after_stat(&quot;p.signf&quot;)) + # Add statistical comparisons
  9. theme_bw()+
  10. scale_x_discrete(labels = function(x) {
  11. num_cars &lt;- mpg %&gt;%
  12. filter(class == c(&quot;compact&quot;, &quot;subcompact&quot;)) %&gt;%
  13. filter(displ == c(2,2.5)) %&gt;%
  14. filter(class %in% x) %&gt;%
  15. group_by(class) %&gt;%
  16. summarise(total_cars = n())%&gt;%
  17. mutate(label = total_cars)
  18. num_cars$label
  19. })
  20. p3

Makes this:

保留 ggplot 中 facet_grid 后的 Scale_x_discrete 刻度标签

When I wanted the tick labels to be 2, 3, 2, 2.

Hopefully someone has an easy answer for this (I am sure it is easy).

Cheers

答案1

得分: 3

  1. 一种选项是为每个面板单独设置标签,同时在使用 `stat_compare_means` 时起作用的方法是使用 `ggh4x::facetted_pos_scales`,它允许在每个面板上单独设置比例尺(包括 `labels`):
  2. ``` r
  3. library(ggplot2)
  4. library(dplyr, warn=FALSE)
  5. library(ggpubr)
  6. library(ggh4x)
  7. x_labels <- mpg %>%
  8. filter(class == c("compact", "subcompact")) %>%
  9. filter(displ == c(2, 2.5)) %>%
  10. count(class, displ)
  11. x_labels <- split(x_labels, x_labels$displ) %>%
  12. lapply(\(x) x |> select(class, n) |> tibble::deframe())
  13. mpg %>%
  14. filter(class == c("compact", "subcompact")) %>%
  15. filter(displ == c(2, 2.5)) %>%
  16. ggplot(aes(x = class, y = hwy, col = class)) +
  17. geom_violin() +
  18. geom_point() +
  19. facet_grid(cols = vars(displ), scales = "free_x") +
  20. stat_compare_means(method = "t.test", comparisons = list(c("compact", "subcompact")), label = after_stat("p.signf")) + # 添加统计比较
  21. theme_bw() +
  22. ggh4x::facetted_pos_scales(
  23. x = list(
  24. displ == 2 ~ scale_x_discrete(labels = x_labels$`2`),
  25. displ == 2.5 ~ scale_x_discrete(labels = x_labels$`2.5`)
  26. )
  27. )

保留 ggplot 中 facet_grid 后的 Scale_x_discrete 刻度标签

  1. <details>
  2. <summary>英文:</summary>
  3. One option to set the labels individually for each panel which at the same time works when using `stat_compare_means` would be to use `ggh4x::facetted_pos_scales` which allows to set the scales (including the `labels`) individually per panel:
  4. ``` r
  5. library(ggplot2)
  6. library(dplyr, warn=FALSE)
  7. library(ggpubr)
  8. library(ggh4x)
  9. x_labels &lt;- mpg %&gt;%
  10. filter(class == c(&quot;compact&quot;, &quot;subcompact&quot;)) %&gt;%
  11. filter(displ == c(2, 2.5)) |&gt;
  12. count(class, displ)
  13. x_labels &lt;- split(x_labels, x_labels$displ) |&gt;
  14. lapply(\(x) x |&gt; select(class, n) |&gt; tibble::deframe())
  15. mpg %&gt;%
  16. filter(class == c(&quot;compact&quot;, &quot;subcompact&quot;)) %&gt;%
  17. filter(displ == c(2, 2.5)) %&gt;%
  18. ggplot(aes(x = class, y = hwy, col = class)) +
  19. geom_violin() +
  20. geom_point() +
  21. facet_grid(cols = vars(displ), scales = &quot;free_x&quot;) +
  22. stat_compare_means(method = &quot;t.test&quot;, comparisons = list(c(&quot;compact&quot;, &quot;subcompact&quot;)), label = after_stat(&quot;p.signf&quot;)) + # Add statistical comparisons
  23. theme_bw() +
  24. ggh4x::facetted_pos_scales(
  25. x = list(
  26. displ == 2 ~ scale_x_discrete(labels = x_labels$`2`),
  27. displ == 2.5 ~ scale_x_discrete(labels = x_labels$`2.5`)
  28. )
  29. )

保留 ggplot 中 facet_grid 后的 Scale_x_discrete 刻度标签<!-- -->

huangapple
  • 本文由 发表于 2023年6月12日 23:59:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76458392.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定