ggplot

我创建了以下数据和图表。如何用符号替换p值?

  • 如果p值大于0.05-不显示它,
  • 如果p值在0.01到0.05之间,则显示*
  • 如果p值小于0.01,则显示**

有包装吗?有人可以举个例子吗?

我发现我可以在stat_compare_means中添加“ label =” p.signif“,但它显示” ns“而不是不显示。

df <- data.frame("Class" = c("A","A","B","C","C"),"Subject" = c("Math","Math","Reading","Writing","Writing"),"Score" = c(round(runif(27,100))))

library("ggplot2")

comp <- list (c("A","B"),c("A",c("B","C"))

ggplot(data = df,aes(x = Class,y = Score,color = Subject)) + 
  facet_wrap(~Subject) +
  geom_violin(trim = FALSE) +
  stat_compare_means(aes(group = Subject),method = "t.test",comparisons = comp)

ggplot

nbayjj 回答:ggplot

我们可以在symnum.args中使用stat_compare_means,并根据需要分配cutpointssymbols

library(ggplot2)
library(ggpubr)

ggplot(data = df,aes(x = Class,y = Score,color = Subject)) + 
   facet_wrap(~Subject) +
   geom_violin(trim = FALSE) +
  stat_compare_means(aes(group = Subject),method = "t.test",comparisons = comp,symnum.args = list(cutpoints = c(0,0.01,0.05,Inf),symbols = c("**","*","")))

enter image description here

,

很明显,您正在使用functions软件包中的ggpubr。添加label = "p.signif",hide.ns = TRUE可以做到:

ggplot(data = df,color = Subject)) + 
  facet_wrap(~Subject) +
  geom_violin(trim = FALSE) +
  stat_compare_means(aes(group = Subject),label = "p.signif",hide.ns = TRUE,comparisons = comp)

enter image description here

本文链接:https://www.f2er.com/3169703.html

大家都在问