将p值或星号添加到ggplot

时间:2018-02-27 12:13:07

标签: r ggplot2 ggpubr

我想要的是与成对的Wilcox测试的成对比较。使用群组:list(c("0.5", "1"),c("1","2"),c("2","3"))。非常简单,起初没有α校正等等。

代码:

data_summary <- function(data, varname, groupnames){
    require(plyr)
    summary_func <- function(x, col){
        c(mean = mean(x[[col]], na.rm=TRUE),
          sd = sd(x[[col]], na.rm=TRUE))
    }
    data_sum<-ddply(data, groupnames, .fun=summary_func,
                    varname)
    data_sum <- rename(data_sum, c("mean" = varname))
    return(data_sum)
}

    df_long <- rbind(ToothGrowth[,-2],data.frame(len=40:50,dose=3.0))
    df_long$ID <- data.table::rowid(df_long$dose)
    df_aggre<- data_summary(df_long, varname="len", 
                        groupnames=c("dose"))

    p<-
    ggplot(df_aggre, aes(x=dose, y=len)) +
        geom_errorbar(aes(ymin=len-sd, ymax=len+sd), width=.1) +
        geom_line() + geom_point() + theme_minimal()

期望的输出。 (我希望红圈中的东西!)

image with sign added

(发明统计结果(例如ns,,*

我的尝试:

library(ggpubr)
p + stat_compare_means(data=df_long,mapping=aes(x=dose, y=len),label = "p.signif", method= "wilcox.test", comparisons=list(c("0.5","1"),c("1","2"),c("2","3")))

    #Error in f(...) : 
    #Can only handle data with groups that are plotted on the x-axis

2 个答案:

答案 0 :(得分:2)

一种简单的方法可以手动添加符号:

  p + annotate("text", x = 0.5, y = 23, label = "ns") +
  annotate("text", x = 1.5, y = 30, label = "**") +
  annotate("text", x = 2.5, y = 48, label = "***") 

答案 1 :(得分:1)

根据this github repo问题,这是包的问题。作者的建议是手动计算p值并将其用作:

geom_signif(annotations = c(formatC(annot_1, digits=3),formatC(annot_2, digits=3)), y_position = c(150, 180), xmin=c(1.2, 0.8), xmax=c(2.2, 1.8))

其中annot_1annot_2是预先计算的p值。