将统计测试与 rstatix 和每个类别的颜色填充相结合

Combine statistical tests with rstatix and color fill per category

我使用这段代码通过 ggplot2 绘制小提琴图:

Cells1 %>%
  ggplot(aes(x=IR_time, y=AreaShape_Area, fill=IR_time)) +
  geom_violin(col=NA) +
  guides(fill=FALSE) + 
  stat_summary(fun.data=data_summary, col = "black") +
  theme_gray() + 
  ggtitle("Cell area after irradiation (3Gy)") +
  ylab("\nArea (pixels)") + 
  xlab("\nDays after exposure to 3Gy\n") +
  scale_fill_manual(values=wes_palette(n=5, name="Moonrise3")) +
  theme(
    plot.title = element_text(size=16),
    axis.title = element_text(size=12, face="bold"),
    axis.text = element_text(size=12))

产生这个情节:

现在我想执行统计检验(这里是成对 t 检验)并将结果添加到图上。我使用 rstatix 包。 所以这是新代码:

stat.test <- Cells1 %>%  pairwise_t_test(AreaShape_Area ~ IR_time, pool.sd=FALSE, p.adjust.method="bonferroni", ref.group="0") %>% 
  add_y_position()

    Cells1 %>%
      ggplot(aes(x=IR_time, y=AreaShape_Area, fill=IR_time)) +
      geom_violin(col=NA) +
      guides(fill=FALSE) + 
      stat_summary(fun.data=data_summary, col = "black") +
      theme_gray() + 
      ggtitle("Cell area after irradiation (3Gy)") +
      ylab("\nArea (pixels)") + 
      xlab("\nDays after exposure to 3Gy\n") +
      scale_fill_manual(values=wes_palette(n=5, name="Moonrise3")) +
      theme(
        plot.title = element_text(size=16),
        axis.title = element_text(size=12, face="bold"),
        axis.text = element_text(size=12)) +
      stat_pvalue_manual(stat.test) 

但它会导致这个错误:

Error: Aesthetics must be either length 1 or the same as the data (4): fill

显然,这来自 aes() 中的 fill=IR_time 参数。 如果我用 fill="blue" 替换它,它就可以工作,我没有错误消息。 但我希望两种颜色都取决于 IR_time 和情节的统计数据。

你知道我该如何解决它吗?

我创建了一个大致相似的同名数据集:

set.seed(69)

Cells1 <- data.frame(
            IR_time = factor(rep(c(0, 2, 4, 8, 12), each = 1000)),
            AreaShape_Area = rgamma(5e3, rep((2:6)^1.8, each = 1e3)) * 3e3)

您的代码需要一个名为 data_summary 的函数,您没有包含该函数,而且似乎也不在我可以在 Google 上找到的任何常见 R 包中。我猜是这样的:

data_summary <- function(x) {
  data.frame(y = mean(x), ymin = mean(x) - sd(x), ymax = mean(x) + sd(x))
}

您没有包含您正在使用的软件包,因此我们需要进行一些侦探工作才能确定我们需要:

library(ggplot2)
library(wesanderson)
library(rstatix)
library(ggpubr)

现在我们可以运行你的代码。据我所知,如果将 stat_pvalue_manual(stat.test) 行更改为 stat_pvalue_manual(data = stat.test, inherit.aes = FALSE) :

,这应该很容易解决
stat.test <- Cells1 %>%  
  pairwise_t_test(AreaShape_Area ~ IR_time, 
                  pool.sd = FALSE, 
                  p.adjust.method = "bonferroni", 
                  ref.group = "0") %>% 
  add_y_position()

Cells1 %>%
  ggplot(aes(x = IR_time, y = AreaShape_Area, fill = IR_time)) +
  geom_violin(col = NA) +
  guides(fill = FALSE) + 
  stat_summary(fun.data = data_summary, col = "black") +
  theme_gray() + 
  ggtitle("Cell area after irradiation (3Gy)") +
  ylab("\nArea (pixels)") + 
  xlab("\nDays after exposure to 3Gy\n") +
  scale_fill_manual(values = wes_palette(n = 5, name = "Moonrise3")) +
  theme(
    plot.title = element_text(size = 16),
    axis.title = element_text(size = 12, face = "bold"),
    axis.text  = element_text(size = 12)) +
  stat_pvalue_manual(data = stat.test, inherit.aes = FALSE) 

reprex package (v0.3.0)

于 2020-09-30 创建