参考:Add P-values and Significance Levels toggplots

ggpubr的包比较局限,能用的test也比较局限,但是做起来快速简单。

当情况特殊时ggpubr就不能用了,可以自己做了显著性test之后再显示在图上。

# show lable in facet grid plot
dat_text <- data.frame()
for (i in names(paired_list)) {
# Compute t-test
res <- t.test(value ~ group, data = paired_list[[i]], paired = TRUE)
dat_text <- rbind(dat_text, data.frame(variable=i, pvalue=res$p.value))
} dat_text$label <- paste("P", round(dat_text$pvalue, 3), sep="=") dat_text[dat_text$pvalue<0.05 & dat_text$pvalue>0.01,]$label <- paste("*",
dat_text[dat_text$pvalue<0.05 & dat_text$pvalue>0.01,]$label, sep=" ") dat_text[dat_text$pvalue<0.01,]$label <- paste("**", "P<0.01", sep=" ") library(ggplot2)
options(repr.plot.width=8, repr.plot.height=12) # 8x8
g2 <- ggplot(data=genes_expr_melt, aes(x=pseudotime, y=value, fill=group, color=group)) +
geom_point(size=0.01, alpha=0.5, aes(color=group, fill=group)) +
labs(x = "Pseudotime", y = "Relative expression", title = "Neuronal lineage") +
geom_smooth(method = 'loess',se=F,size=0.15,span = 0.7) + # ,alpha=0.05, weight=0.1,
facet_wrap( ~ variable, ncol=3, labeller = label_context, scales = "free_y") + #
geom_text(size = 5, data = dat_text, mapping = aes(x = Inf, y = Inf, label = label),
hjust = 1.05,vjust = 1.5, color=ifelse(dat_text$pvalue < 0.05,'red','black')) +
# themes
theme(strip.background = element_blank(),
panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.spacing=unit(.4, "lines"),panel.border = element_rect(color = "black", fill = NA, size = 0.5))+
theme(axis.text.x = element_text(face="plain", angle=0, size = 10, color = "black", vjust=0.5),
axis.text.y = element_text(face="plain", size = 10, color = "black"),
axis.title =element_text(size = 15)) +
theme(strip.background = element_rect(fill = "gray90", color = NA))+
# theme(legend.position = "none") + # must remove legend
theme(strip.placement = "outside", strip.text.x = element_text(face="plain", size = 13),
strip.text.y = element_text(face="plain", size = 11)) +
theme(strip.text.x = element_text(margin = margin(1,0,1,0, "mm"))) +
scale_color_manual(values=c("deepskyblue","red","gray50")) +
scale_fill_manual(values=c("deepskyblue","red","gray50"))
plot(g2)
# }

多组比较,挑选感兴趣的显示显著性。

data("ToothGrowth")
head(ToothGrowth)  
library(ggpubr)
my_comparisons <- list( c("0.5", "1"), c("1", "2"), c("0.5", "2") )
options(repr.plot.width=4, repr.plot.height=4)
ggplot(ToothGrowth, aes(x=as.character(dose), y=len, fill=dose)) +
geom_boxplot(outlier.size=NA, size=0.01, outlier.shape = NA) +
geom_jitter(width = 0.3, size=0.01) +# , aes(color=supp) +
stat_compare_means(comparisons = my_comparisons)+ # Add pairwise comparisons p-value
stat_compare_means(label.y = 50, label.x = 1.5) # Add global p-value

还可以设定一个ref group来显示显著性差异,只需要改一下设定。

  stat_compare_means(method = "anova", label.y = 1.3, label.x = 3)+ # Add pairwise comparisons p-value
# # Add global p-value
stat_compare_means(label = "p.signif", method = "t.test", ref.group = "hNP-D20", label.y = 1.1) +

  

生物学的强烈推荐看看Y叔的公众号里的统计相关的文章,非常的基础和实用。

统计

代码例子:

options(repr.plot.width=7, repr.plot.height=6)
# facet boxplot
bp <- ggplot(expr_data2, aes(x=group, y=expression, fill=NA)) +
geom_boxplot(outlier.size=NA, size=0.01, outlier.shape = NA) +
geom_jitter(width = 0.3, size=0.01, aes(color=cluster)) +
# + geom_boxplot( +
facet_grid( cluster ~ gene, switch="y") + # , scales = "free"
theme_bw() +
stat_compare_means(aes(group = group, label = ..p.signif..), label.x = 1.3,label.y = 1.3,
method = "wilcox.test", hide.ns = T) + # label = "p.format",
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
labs(x = "", y = "", title = "") +
theme(panel.spacing=unit(.3, "lines"),panel.border = element_rect(color = "black", fill = NA, size = 0.2)) +
theme(axis.ticks.x = element_blank(), axis.ticks = element_line(size = 0.1),
axis.text.x = element_text(face="plain", angle=90, size = 8, color = "black", vjust=0.5),
axis.text.y = element_text(face="plain", size = 4, color = "black"),
axis.title =element_text(size = 12)) +
theme(strip.background = element_rect(fill = "gray97", color = NA))+
theme(legend.position = "none") +
theme(strip.placement = "outside", strip.text.x = element_text(face="italic", size = 11),
strip.text.y = element_text(face="plain", size = 11)) +
scale_y_continuous(position="right", limits = c(-0.5,1.5)) +
scale_fill_manual(values=brewer.pal(8,"Set2")[c(2,3,7,1,5,6)]) +
scale_color_manual(values=brewer.pal(8,"Set2")[c(2,3,7,1,5,6)])
bp

  

ggplot的boxplot添加显著性 | Add P-values and Significance Levels to ggplots | 方差分析的更多相关文章

  1. thinkphp添加数据 add()方法

    thinkphpz内置的add()方法用于向数据库表添加数据,相当于SQL中的INSERT INTO 行为添加数据 add 方法是 CURD(Create,Update,Read,Delete / 创 ...

  2. [Swift]LeetCode921.使括号有效的最少添加 | Minimum Add to Make Parentheses Valid

    Given a string S of '(' and ')' parentheses, we add the minimum number of parentheses ( '(' or ')', ...

  3. menu-代码添加以及add方法参数意义

    今天需要给一个menu动态添加一个item,先把方法记录如下 @Override public boolean onCreateOptionsMenu(Menu menu) { menu.add(Me ...

  4. LeetCode 921. 使括号有效的最少添加(Minimum Add to Make Parentheses Valid) 48

    921. 使括号有效的最少添加 921. Minimum Add to Make Parentheses Valid 题目描述 给定一个由 '(' 和 ')' 括号组成的字符串 S,我们需要添加最少的 ...

  5. [deviceone开发]-动态添加组件add方法的示例

    一.简介 这个示例详细介绍ALayout的add方法的使用(原理也适用于Linearlayout),以及add上去的新ui和已有的ui如何数据交换,初学者推荐.二.效果图 三.相关下载 https:/ ...

  6. 差异基因分析:fold change(差异倍数), P-value(差异的显著性)

    在做基因表达分析时必然会要做差异分析(DE) DE的方法主要有两种: Fold change t-test fold change的意思是样本质检表达量的差异倍数,log2 fold change的意 ...

  7. magento产品成功添加到购物车后跳转到不同页面 添加 add to cart 按钮

    1 添加产品到购物车成功后是跳转到购物车页面或不跳转.这个在后台可以设置 system -> configuration -> After Adding a Product Redirec ...

  8. 从零开始编写自己的C#框架(22)——添加普通列表页面

    普通列表页面指的是上一章那种有层次感列表以外的正常列表页面,由于上一章已讲解了正常添加页面的相关操作了,所以部分相关的操作本章节就不再罗嗦重复一次了.大家可以试试先用本章内容中的一些简单介绍,自己使用 ...

  9. 下拉刷新列表添加SwipeDismissListViewTouchListener实现滑动删除某一列。

    <Android SwipeToDismiss:左右滑动删除ListView条目Item> Android的SwipeToDismiss是github上一个第三方开源框架(github上的 ...

随机推荐

  1. mysql----------mysql的一些常用命令

    1.查询一张表中某个字段重复值的记录 select id,cert_number from (select id,cert_number,count(*)as n from 表明 group by c ...

  2. Yarn 组件的指挥部 – 调度器Scheduler

    linux基础 为hadoop集群的搭建扫清了障碍,也为内存的管理,文件系统的管理扫清了障碍 接着到Hadoop的阶段,首先做集群的安装,深入到使用这两个核心的组件,分布式文件系统HDFS,解决大量数 ...

  3. Zepto源码分析之一(代码结构及初始化)

    关于读源码,读jQuery自然是不错,但太过于庞大不易解读,对于小白,最好从Zepto,Lodash这样的小库入手. 这里使用的是zepto1.1.6版本为例. 自执行函数 在阅读之前,先弄清楚闭包和 ...

  4. spring boot入门小案例

    spring boot 入门小案例搭建 (1) 在Eclipse中新建一个maven project项目,目录结构如下所示: cn.com.rxyb中存放spring boot的启动类,applica ...

  5. beego 初体验 - 上传文件

    页面: controller: 将form表单文件上传到本地,并保存.

  6. Jmeter笔记(Ⅲ) Jmeter的非GUI操作

    在启动Jmeter时,我们会看到这样一句提示: 不要使用GUI模式(界面模式)进行负载测试,GUI模式只能用于创建测试和调试.进行负载测试时,需要时用非GUI模式. 那么为什么进行负载测试时一定要用非 ...

  7. Log4j 2使用教程二 【详解】

    配置 Log4j 2的配置可以通过4种方式中的1种完成: 1.通过使用XML,JSON,YAML或属性格式编写的配置文件. 2.以编程方式,通过创建一个ConfigurationFactory和配置实 ...

  8. 基于PLC1850平台的ICMP包请求与响应

    一.以太网IP包报文格式 IP包是连接在以太网首部(以太网目的MAC地址(6个字节)+以太网源MAC地址(6个字节)+帧类型(2个字节))之后. IP报文中各个字段分析如下: ①.版本:在IP报文中, ...

  9. cxf整合spring中出现的错误

    Caused by: java.lang.ClassNotFoundException: javax.wsdl.extensions.ElementExtensible at org.apache.c ...

  10. 编译原理---antlr实践+编译过程理解+课程理解知识点

    0.其他说明 0.0编译器分为前.中.后端,课上主要学的是前端.前端又分为词法分析(lexical analysis).语法分析(syntax analysis).语义分析(semantic anal ...