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:

相关推荐
zhanghongyi_cpp7 小时前
当当网Top500书籍信息爬取与分析
python·网络爬虫·数据可视化
Expecto01 天前
因子分析——数学原理及R语言代码
算法·r语言·统计学·多元统计分析
人类群星闪耀时1 天前
R语言数据挖掘:从“挖井”到“淘金”
开发语言·数据挖掘·r语言
How_doyou_do1 天前
项目实战-25年美赛MCM/ICM-基于数学建模与数据可视化的动态系统模型
python·数学建模·数据可视化
郭不耐2 天前
DeepSeek智能时空数据分析(九):NL2SQL绘制河流名字-如何给轨迹添加说明文字
信息可视化·数据分析·aigc·数据可视化·大屏端
前端小崔3 天前
从零开始学习three.js(14):一文详解three.js中的粒子系统Points
开发语言·前端·javascript·学习·3d·webgl·数据可视化
Jet45053 天前
第100+40步 ChatGPT学习:R语言实现多轮建模
学习·chatgpt·r语言·多轮建模
Tiger Z3 天前
R 语言科研绘图第 45 期 --- 桑基图-和弦
开发语言·r语言·贴图
敢敢のwings3 天前
论文速读《Embodied-R: 基于强化学习激活预训练模型具身空间推理能力》
开发语言·r语言
清同趣科研4 天前
R绘图|3分钟复现瑞士“苏黎世大学”Nature全球地图——基于R包ggplot2+sf等
开发语言·r语言