1

我想做这样的事情

使用 ggsignif 或 ggpubr 为 x 轴上没有标签的子组添加多重比较

我做到了这一点:

包和示例数据

library(tidyverse)
library(ggpubr)
library(ggpol)
library(ggsignif)

example.df <- data.frame(species = sample(c("primate", "non-primate"), 50, replace = TRUE),
                         treated = sample(c("Yes", "No"), 50, replace = TRUE),
                         gender = sample(c("male", "female"), 50, replace = TRUE), 
                         var1 = rnorm(50, 100, 5))

级别

example.df$species <- factor(example.df$species, 
                             levels = c("primate", "non-primate"), labels = c("p", "np"))
example.df$treated <- factor(example.df$treated, 
                             levels = c("No", "Yes"), labels = c("N","Y"))
example.df$gender <- factor(example.df$gender, 
                            levels = c("male", "female"), labels = c("M", "F"))

因为当他们ggsignif需要ggpubr引用的组没有在 x 轴上明确命名时(因为它们是 x 轴上每个变量的子组并且是仅在填充图例中而不是 x 轴中指示,我尝试了这个。

example.df %>% 
  unite(groups, species, treated, remove = F, sep= "\n") %>% 
  {ggplot(., aes(groups, var1, fill= treated)) + 
     geom_boxjitter() +
     facet_wrap(~ gender, scales = "free") +
     ggsignif::geom_signif(comparisons =  combn(sort(unique(.$groups)), 2, simplify = F),
                           step_increase = 0.1)}

我明白了,

具有为每个组计算的显着性值的分面图 具有为每个组计算的显着性值的分面图

但是,x 轴上组合组的顺序不是我想要的。我想为每个方面订购 p/N、np/N、p/Y、np/Y。

我该怎么做呢?任何帮助是极大的赞赏。

编辑:使用 mutate 创建一个新变量,并使用我喜欢的绘图顺序解决它使其成为有序因子。

example.df %>% 
  unite(groups, species, treated, remove = F, sep= "\n") %>% 
  mutate(groups2 = factor(groups, levels = c("p\nN", "np\nN", "p\nY", "np\nY"),
                          ordered = TRUE)) %>% 
  {ggplot(., aes(groups2, var1, fill= treated)) +
     geom_boxjitter() + 
     facet_wrap(~gender,scales = "free") +
     ggsignif::geom_signif(comparisons = combn(sort(unique(.$groups2)), 2, simplify = F), 
                           step_increase = 0.1)}

但是我仍在寻找完全不必使用unite并保留原始因子并仍然获得显着性值以使用ggsignifor绘制的解决方案ggpubr

4

1 回答 1

1

(来自基本包)的默认参数interaction似乎给出了您正在寻找的因子排序:

结果

example.df %>%
  mutate(groups = interaction(species, treated, sep = "\n")) %>%
  {ggplot(., aes(groups, var1, fill= treated)) + 
    geom_boxjitter() +
    facet_wrap(~ gender, scales = "free") +
    geom_signif(comparisons = combn(sort(as.character(unique(.$groups))), 2, simplify = F),
                step_increase = 0.1)}
于 2019-02-13T02:40:47.470 回答