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:

相关推荐
极光代码工作室3 天前
基于数据仓库的电商数据分析平台
大数据·hadoop·python·spark·数据可视化
GZ同学3 天前
单双变量Ripley’s K函数 R 语言实现
开发语言·r语言
柳杉4 天前
我用Threejs 搓了一个 3D 中国地图设计器,开箱即用
前端·three.js·数据可视化
popcorn_min5 天前
共享单车需求预测:时间特征工程 + 随机森林,R² 达到 0.931
随机森林·r语言·kotlin
小的博客6 天前
Oh-My-Posh安装及使用
学习·数据可视化
周庆猛8 天前
Babylon.js 多灯场景在 Windows 上报错:VERTEX shader uniform block count exceeds GL_MAX_VE
前端·数据可视化
一晌小贪欢8 天前
第26节:自动化办公——利用 Python 自动生成动态分析报告 (PPT/PDF)
开发语言·python·数据分析·自动化·powerpoint·pandas·数据可视化
山海鲸实战案例分享8 天前
【数字孪生实战案例】怎样为二维孪生组件新增测绘功能?~山海鲸可视化
数字孪生·数据可视化·零代码·测绘·实战案例·山海鲸可视化·二维孪生
小哈机器人9 天前
Phantom Bridge:一个基于WebRTC的ROS2远程可视化与遥操作工具
机器人·webrtc·数据可视化