当前位置: 首页 > article >正文

R语言 | 在图形上标注P值的R包

遇到一个记录一个。

ggsignif: 多组之间的比较

#BiocManager::install('ggsignif')
library("ggsignif")
library('ggplot2')
# geom_signif()
# compare_means(value ~ group, data = exprSet_L,method="wilcox.test", paired=FALSE)
exprSet_L=mydata
gene_name='geneXX'

my_comparisons <- list(c("T1","T2"), c("T1","T3"),c("T1", "T4"))
ggplot(exprSet_L,aes(group,value))+
  geom_boxplot(width=0.5)+
  geom_jitter(aes(color=group))+
  theme(plot.title=element_text(size = 25),
        axis.text.x=element_text(size=25,angle=0),
        axis.text.y=element_text(size=25),
        axis.title.x=element_text(size = 25),
        axis.title.y=element_text(size = 25))+
  labs(title=paste0('Title:',gene_name),x=gene_name, y= 'Expression')+
  geom_signif(comparisons = my_comparisons,
              step_increase = 0.1,
              map_signif_level = T, #T显示星号,F显示p值
              test = t.test, size=1,textsize = 6)+
  theme_set(theme_set(theme_bw(base_size=22)))

iris 实例

library(ggplot2)
library("ggsignif")

#' Boxplot with p value
#' 
#' v0.1
#' v0.2 自定义设置颜色、统计学方法等
#'
#' @param dat 输入数据框:前几列是数据,比如身高、体重、肺活量; 最后一列是分组(如小学、初中、高中)
#' @param i 数据第i列,除最后一行外的列编号
#' @param my_comparisons 两两比较
#' @param test.method 两两比较的统计学方法,默认是 wilcox.test。还可以是 t.test
#' @param title 图标题
#' @param ylab y轴标题
#' @param cols 分组颜色,要和分组一致
#' @param legend.name 图例标题 //为啥不起作用?
#' @param alpha 小提琴图的不透明度,[0,1]。越小越透明
#' @param border.color 小提琴图 边框颜色,默认为黑色,我设置为无色
#' @param seed 随机数种子,用于生成 jitter 点的抖动位置
#'
#' @return
#' @export
#'
#' @examples
drawBoxPlot=function(dat, i, my_comparisons,
                     test.method=c("wilcox.test", "t.test")[2],
                     cols=c("#307EC1", "orange", "deeppink"),
                     title="", ylab="value", legend.name="",
                     alpha=0.5, border.color="#11223300", seed=2025){
  #i=1
  message("> test.method:", test.method)
  message("> my_comparisons:", my_comparisons)
  # 取第i列和最后一列,给列名
  d1=dat[,c(i, ncol(dat))] 
  colnames(d1)=c("value", "group")
  # 绘图
  set.seed(seed)
  p2=ggplot(d1, aes(group, value))+
    geom_violin( aes(fill=group), 
                 scale="width", 
                 color=border.color,
                 show.legend = F, 
                 alpha=alpha)+ #不透明度[0,1]
    geom_jitter(aes(color=group), width=0.2, size=0.5)+
    geom_boxplot(width=0.15, outliers = F, fill="white", alpha=0.7)+
    theme_classic(base_size = 12)+
    theme(
      axis.text.x = element_text(angle=45, hjust=1, size=12),
      axis.text.y = element_text(size=10),
      plot.title=element_text(size = 10),
    )+
    guides(color = guide_legend(override.aes = list(size = 3)))+
    scale_color_manual(name=legend.name, values=cols)+
    scale_fill_manual(values=cols)+
    #labs(title=paste0('Title:',gene_name),x=gene_name, y= 'Expression')+
    labs(x="",
         y=ylab, 
         title=title )+
    geom_signif(comparisons = my_comparisons,
                step_increase = 0.18,
                #y_position=max(d1[,1])+0.02,
                #test = t.test, 
                test = test.method,  #"wilcox.test", "t.test"
                map_signif_level = F, #T显示星号,F显示p值
                size=0.5, textsize = 3)#+
  #ylim(y=c(min(d1[,1]), max(d1[,1])+0.1)  )
  #theme_set(theme_set(theme_bw(base_size=22)))
  return(p2)
}
if(0){
  # How to use
  # 1.准备数据:前几列是数据,最后一列是分组。
  # 每次取一列数据(第i列)和最后的分组,用于绘图
  dat=iris
  table(dat[, ncol(dat)])
  # setosa versicolor  virginica
  #     50         50         50
  
  # 2. 手动设置分组: 用于两两t-检验
  my_comparisons <- list(c("versicolor","setosa"), c("virginica","setosa"), c("virginica", "versicolor"))

  # 3. 保存每列的小图到list中
  plots=list()
  for(i in 1:(ncol(dat)-1)){
    #i=1
    p1=drawBoxPlot(dat, i, my_comparisons, 
                title=colnames(dat)[i],
                ylab = paste0("Length of ", colnames(dat)[i] ), 
                #cols=c("#307EC1", "orange", "deeppink"),
                alpha=0.4 )
    plots[[i]]=p1
  }
  
  # 4.写到文件:设置小图列数,共用一个图例
  outputRoot="D://"
  pdf(paste0(outputRoot, "_Boxplot.pdf"), width=5, height=7)
  patchwork::wrap_plots(plots, ncol = 2, guides = "collect")
  dev.off()
}

在这里插入图片描述
右图是全貌,左图是其第一个小图。

ggpubr::stat_compare_means() 两组的比较

#install.packages("ggpubr")

library(ggplot2)
library(ggpubr)

  stat_compare_means(aes(group = location),
                     ref.group="nLung",
                     #label = "p.signif", 
                     label = "p.format",
                     method = "t.test", 
                     paired=F,
                     #label.y =5, 
                     show.legend = F)

Ref:

  • https://blog.csdn.net/ZaoJewin/article/details/135150515
  • https://blog.csdn.net/jiangshandaiyou/article/details/135956610
  • https://blog.csdn.net/weixin_65744007/article/details/127381702
  • https://www.datanovia.com/en/blog/how-to-add-p-values-to-ggplot-facets/
  • https://stackoverflow.com/questions/70883042/adding-p-values-to-ggplot-facets-with-different-scales

http://www.kler.cn/a/571921.html

相关文章:

  • 【多模态目标检测】【AAAI 2025】FD2-Net
  • PyCharm 的详细安装步骤(以 Windows 系统为例,其他系统类似)
  • vue3(笔记)4.0 vueRouter.导航守卫.ElementPuls知识点
  • NetMizer dologin.php sql注入漏洞复现(XVE-2024-37672)(附脚本)
  • MySQL--共享锁和排他锁
  • AF3 block_diag函数解读
  • QTcpSocket使用指南与实践
  • Unity摄像机跟随物体
  • 2025年03月04日Github流行趋势
  • 深入理解 Android MediaCodec 视频编码
  • React进阶之前端业务Hooks库(六)
  • 遗传算法详解及在matlab中的使用
  • SSM记忆旅游网站
  • 基于 RBAC 的前端权限管理实现教程
  • ADB、Appium 和 大模型融合开展移动端自动化测试
  • 路由基础学习
  • 清华团队提出HistoCell,从组织学图像推断超分辨率细胞空间分布助力癌症研究|顶刊精析·25-03-02
  • 自由学习记录(40)
  • 基于微信小程序的停车场管理系统的设计与实现
  • Tomcat:Java Web应用的强大支撑