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

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

Keeping the Scale_x_discrete tick labels in ggplot after facet_grid

问题

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

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

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

library(tidyverse)
library(ggpubr)

p3 <- mpg %>%
  filter(class == c("compact", "subcompact")) %>%
  filter(displ == c(2, 2.5)) %>%
  ggplot(aes(x = class, y = hwy, col = class)) +
  geom_violin() +
  geom_point() +
  #facet_grid(cols = vars(displ))+
  stat_compare_means(method = "t.test", comparisons = list(c("compact", "subcompact")), label = after_stat("p.signf")) +  # Add statistical comparisons
  theme_bw() +
  scale_x_discrete(labels = function(x) {
    num_cars <- mpg %>%
      filter(class == c("compact", "subcompact")) %>%
      filter(displ == c(2, 2.5)) %>%
      filter(class %in% x) %>%
      group_by(class) %>%
      summarise(total_cars = n()) %>%
      mutate(label = total_cars)
    num_cars$label
  })
p3

这将创建以下图表:

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

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

p3 <- mpg %>%
  filter(class == c("compact", "subcompact")) %>%
  filter(displ == c(2, 2.5)) %>%
  ggplot(aes(x = class, y = hwy, col = class)) +
  geom_violin() +
  geom_point() +
  facet_grid(cols = vars(displ)) +
  stat_compare_means(method = "t.test", comparisons = list(c("compact", "subcompact")), label = after_stat("p.signf")) +  # Add statistical comparisons
  theme_bw() +
  scale_x_discrete(labels = function(x) {
    num_cars <- mpg %>%
      filter(class == c("compact", "subcompact")) %>%
      filter(displ == c(2, 2.5)) %>%
      filter(class %in% x) %>%
      group_by(class) %>%
      summarise(total_cars = n()) %>%
      mutate(label = total_cars)
    num_cars$label
  })
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:

library(tidyverse)
library(ggpubr)

p3 &lt;- mpg %&gt;%
  filter(class == c(&quot;compact&quot;, &quot;subcompact&quot;)) %&gt;%
  filter(displ == c(2,2.5)) %&gt;%
  ggplot(aes(x=class, y=hwy, col=class))+
  geom_violin()+
  geom_point()+
  #facet_grid(cols = vars(displ))+
  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
  theme_bw()+
  scale_x_discrete(labels = function(x) {
    num_cars &lt;- mpg %&gt;%
      filter(class == c(&quot;compact&quot;, &quot;subcompact&quot;)) %&gt;%
      filter(displ == c(2,2.5)) %&gt;%
      filter(class %in% x) %&gt;%
      group_by(class) %&gt;%
      summarise(total_cars = n())%&gt;%
      mutate(label = total_cars)
    num_cars$label
  })
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.

p3 &lt;- mpg %&gt;%
  filter(class == c(&quot;compact&quot;, &quot;subcompact&quot;)) %&gt;%
  filter(displ == c(2,2.5)) %&gt;%
  ggplot(aes(x=class, y=hwy, col=class))+
  geom_violin()+
  geom_point()+
  facet_grid(cols = vars(displ))+
  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
  theme_bw()+
  scale_x_discrete(labels = function(x) {
    num_cars &lt;- mpg %&gt;%
      filter(class == c(&quot;compact&quot;, &quot;subcompact&quot;)) %&gt;%
      filter(displ == c(2,2.5)) %&gt;%
      filter(class %in% x) %&gt;%
      group_by(class) %&gt;%
      summarise(total_cars = n())%&gt;%
      mutate(label = total_cars)
    num_cars$label
  })
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

一种选项是为每个面板单独设置标签,同时在使用 `stat_compare_means` 时起作用的方法是使用 `ggh4x::facetted_pos_scales`,它允许在每个面板上单独设置比例尺(包括 `labels`):

``` r
library(ggplot2)
library(dplyr, warn=FALSE)
library(ggpubr)
library(ggh4x)

x_labels <- mpg %>%
  filter(class == c("compact", "subcompact")) %>%
  filter(displ == c(2, 2.5)) %>%
  count(class, displ)

x_labels <- split(x_labels, x_labels$displ) %>%
  lapply(\(x) x |> select(class, n) |> tibble::deframe())

mpg %>%
  filter(class == c("compact", "subcompact")) %>%
  filter(displ == c(2, 2.5)) %>%
  ggplot(aes(x = class, y = hwy, col = class)) +
  geom_violin() +
  geom_point() +
  facet_grid(cols = vars(displ), scales = "free_x") +
  stat_compare_means(method = "t.test", comparisons = list(c("compact", "subcompact")), label = after_stat("p.signf")) + # 添加统计比较
  theme_bw() +
  ggh4x::facetted_pos_scales(
    x = list(
      displ == 2 ~ scale_x_discrete(labels = x_labels$`2`),
      displ == 2.5 ~ scale_x_discrete(labels = x_labels$`2.5`)
    )
  )

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


<details>
<summary>英文:</summary>

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:

``` r
library(ggplot2)
library(dplyr, warn=FALSE)
library(ggpubr)
library(ggh4x)

x_labels &lt;- mpg %&gt;%
  filter(class == c(&quot;compact&quot;, &quot;subcompact&quot;)) %&gt;%
  filter(displ == c(2, 2.5)) |&gt; 
  count(class, displ)

x_labels &lt;- split(x_labels, x_labels$displ) |&gt; 
  lapply(\(x) x |&gt; select(class, n) |&gt; tibble::deframe())

mpg %&gt;%
  filter(class == c(&quot;compact&quot;, &quot;subcompact&quot;)) %&gt;%
  filter(displ == c(2, 2.5)) %&gt;%
  ggplot(aes(x = class, y = hwy, col = class)) +
  geom_violin() +
  geom_point() +
  facet_grid(cols = vars(displ), scales = &quot;free_x&quot;) +
  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
  theme_bw() +
  ggh4x::facetted_pos_scales(
    x = list(
      displ == 2 ~ scale_x_discrete(labels = x_labels$`2`),
      displ == 2.5 ~ scale_x_discrete(labels = x_labels$`2.5`)
    )
  )

保留 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:

确定