【问题标题】:How to add p values into grouped charts in ggplot?如何将p值添加到ggplot中的分组图表中?
【发布时间】:2021-10-22 06:08:35
【问题描述】:

当我想在我的图中添加 p 值时:

library(tidyverse)
library(ggpubr)
library(rstatix)

 stat.test3 <- MP %>%
    group_by(TBI) %>%
    wilcox_test(age ~ mp_1) %>%
    adjust_pvalue(method = "bonferroni") %>%
    add_significance("p.adj")%>%
  mutate(y.position = 35)

 C2<-  ggplot(data=MP, aes(x=TBI, y=age, fill=mp_1))+
    geom_violin()+
    geom_boxplot(width=.2, fatten=NULL, position = position_dodge(0.9))+
    stat_summary(fun="median", geom="point", position = position_dodge(0.9))+
    stat_summary(fun.data = "mean_se", geom = "errorbar", width=.1, position = position_dodge(0.9))+
      scale_fill_brewer(name="Mind-pop", palette = "Accent")

  C2+ stat_pvalue_manual(stat.test3, xmin = "TBI",xmax = NULL)

它给了我这个错误:

FUN(X[[i]], ...) 中的错误:找不到对象“mp_1” 将 stat_pvalue 添加到对象后会显示此错误。 我该如何解决?

【问题讨论】:

  • 您能否与我们分享您的数据样本? (dput(MP, 10))
  • 年龄 55 54 56 60 55 53 61 56 58 58 56 58 58 58 59 57 56 60 57 58 61 60 TBI TBI TBI TBI TBI TBI TBI TBI TBI TBI TBI TBI TBI HC HC HC HC HC HC HC HC HC HC HC mp_1 是 是 是 是 是 否 是 是 是 是 是 是 是 否 是 是 是 是 是 是 是 是 是

标签: r ggplot2 p-value ggpubr violin-plot


【解决方案1】:

我不熟悉ggpubr,所以不能说我了解根本问题,但似乎color=mp_1 而不是fill=mp_1 可能会解决您的问题。这是在以下行中:

C2 &lt;- ggplot(data=MP, aes(x=TBI, y=age, color=mp_1)).

完整的代码如下。我还更改了y.position,以便重要性位于情节的顶部。

MP <- structure(list(age = c(55L, 54L, 56L, 60L, 55L, 53L, 61L, 56L, 
                             58L, 58L, 56L, 58L, 58L, 58L, 59L, 57L, 56L, 60L, 57L, 58L, 61L, 
                             60L),
                     mp_1 = structure(c(2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 
                            2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("No", "Yes"), class = "factor"), 
                     TBI = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("HC", 
                "TBI"), class = "factor")), class = "data.frame", row.names = c(NA, -22L))

library(tidyverse)
library(ggpubr)
library(rstatix)
    
stat.test3 <- MP %>%
  group_by(TBI) %>%
  wilcox_test(age ~ mp_1) %>%
  adjust_pvalue(method = "bonferroni") %>%
  add_significance("p.adj") %>%
  mutate(y.position = 61.5)

C2 <-  ggplot(data=MP, aes(x=TBI, y=age, color=mp_1))+
  geom_violin() +
  geom_boxplot(width=.2, fatten=NULL, position = position_dodge(0.9))+
 stat_summary(fun="median", geom="point", position = position_dodge(0.9))+
 stat_summary(fun.data = "mean_se", geom = "errorbar", width=.1, position = position_dodge(0.9))+
 scale_fill_brewer(name="Mind-pop", palette = "Accent")

C2 + stat_pvalue_manual(stat.test3, xmin = "TBI",xmax = NULL)

【讨论】:

  • 感谢您的宝贵时间。我想对小提琴图进行分组,因此我使用了填充。
  • ggviolin 会解决您想要实现的目标吗?如ggviolin(data=MP, "TBI", "age", fill = "mp_1", add = "boxplot") + stat_pvalue_manual(stat.test3, xmin = "TBI",xmax = NULL)
猜你喜欢
  • 1970-01-01
  • 2018-08-07
  • 1970-01-01
  • 2016-09-28
  • 2020-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多