使用 dplyr 中的 unite 进行 ggplot 绘图时指定组的顺序

Specify the order for groups when using unite from dplyr for plotting with ggplot

我想做这样的事情

我走到这一步:

包和示例数据

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

因为我没有运气让 ggsignifggpubr 正确放置重要的组,因为他们需要引用的组没有在 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 并保留原始因子并仍然使用 ggsignifggpubr 绘制显着性值的解决方案。

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