美文网首页R
内有箱线图的小提琴图(加检验)

内有箱线图的小提琴图(加检验)

作者: ytbao | 来源:发表于2021-11-16 10:38 被阅读0次
    R
    library(ggplot2)
    install.packages("ggpubr")
    library(ggpubr)
    
    df<-read.table("1.txt",header=F)
    head(df)
         V1   V2
    1 shabi  620
    2 shabi  225
    3 caiji 1753
    4 caiji  372
    5 shabi  426
    6 caiji 3514
    
    pdf("1.pdf",width = 3.5, height = 2)
    ggviolin(df, x="V1", y="V2", fill = "V1", palette = c("#F7B32D", "#7BDFF2"), 
    add = "boxplot", add.params = list(fill="white"))+ theme(legend.position="none") +scale_y_log10()+
    geom_signif(comparisons = list(c("Donar", "Free")),map_signif_level=T,
    vjust=0.5,color="black", textsize=4,test=wilcox.test,step_increase=0.1,margin_top=0.12)+
    ylab("1")+ xlab("2")
    dev.off()
    
    theme(legend.position="none")  ggplot2中移除图例
    
    geom_signif 函数:
    geom_signif(comparisons = NULL,         #设置比对的组,用一个每个元素长度为2的列表,可以设置多组。
                data=NULL,                  #设置数据,如果NULL则从ggplot继承,也可以自己指定。
                position="identity"         #位置调整
                step_increase=0,            #如果有多个比对,每个比对错开率
                map_signif_level = T,       #是否展示显著水平的匹配,c("***"=0.001, "**"=0.01, "*"=0.05)
                test = "wilcox.test",       #检验的方式,可以t.test等
                test.args= NULL,            #检验函数的额外参数
                margin_top=0.05,           #位置比最大值高出的位置,百分比,如果是多个比对,可以用向量分别表示
                textsize = 3.88             #字体大小
                )
    
    + stat_compare_means(aes(group=V1),label = "p.signif",hide.ns = TRUE)
    添加显著性检验的P值也可用ggpubr包stat_compare_means()函数,指定根据那一列来分组,也可以自己指定不同组间的比较,
    如my_comparisons <- list(c("x1", "x2"), c("x4", "x5"))
    p+stat_compare_means(comparisons = my_comparisons)
    
    stat_compare_means()默认Wilcoxon Rank Sum and Signed Rank Tests,如果要用t检验指定method参数
    method = "t.test"
    
    如果想把P值改成星号,加label=“p.signif”参数
    不显著会在图上显示ns,如果不想要ns,可以加hide.ns = TRUE参数
    
    星号的位置可以手动指定,用label.y = c(xx,xx)参数,xx是指星号纵坐标的位置,后面AI弄也行
    
    添加线段,可用ggsignif包geom_signif()函数,用法 小明大佬 https://www.jianshu.com/p/eea6c626569e)
    有介绍
    

    参考:
    https://www.jianshu.com/p/eea6c626569e 小明的数据分析笔记本 R分组箱线图添加显著性标记
    https://blog.csdn.net/xj4math/article/details/115448669 R通过ggpubr包添加p-value和显著性标记
    https://zhuanlan.zhihu.com/p/259280570 为箱线图添加显著性注释

    相关文章

      网友评论

        本文标题:内有箱线图的小提琴图(加检验)

        本文链接:https://www.haomeiwen.com/subject/fsthtrtx.html