R绘图做生物学显著性统计,然后添加显著性标记,但是都是手动,比较繁琐,好在ggplot2有个扩展包ggsignif,提供更加人性化的方法。
参数说明:
y_position: 是短横线在y轴上的高度;
xmin :为短横线最左端的x轴上的位置;
xmax :为短横线最右端的x轴上的位置;
tip_length :短横线下折线的长度;
vjust :p值或者显著性离短横线的距离,负数在线上,正数在线下;
map_signif_level :T为显著性,F为p value;
sprintf :调用C函数sprintf,可以用来格式化字符串。
rm(list = ls())
#调出说明文档
??ggsignif
library(ggplot2)
library(ggsignif)
head(iris)
#自动添加显著性标记
compaired <- list(c("versicolor", "virginica"))
ggplot(iris, aes(x=Species, y=Sepal.Width, fill=Species)) +
geom_boxplot() +
geom_signif(comparisons = compaired,
map_signif_level = T)
#手动添加显著性标记
dat <- data.frame(Group = c("S1","S1","S2","S2"),
Sub = c("A","B","A","B"),
Value = c(3,5,7,8))
dat
ggplot(dat, aes(Group, Value)) +
geom_bar(aes(fill = Sub), stat = "identity", position = "dodge", width = .5) +
geom_signif(y_position = c(5.3, 8.3), xmin = c(0.8, 1.8), xmax = c(1.2, 2.2),
annotations = c("**","NS"), tip_length = 0) +
geom_signif(comparisons = list(c("S1","S2")),
y_position = 9.3, tip_length = 0, vjust = 0) +
scale_fill_manual(values = c("grey80","grey20"))
??geom_signif
ggplot(mpg, aes(class, hwy, fill= class)) +
geom_boxplot() +
geom_signif(comparisons = list(c("compact", "pickup"),
c("subcompact", "suv")))
ggplot(mpg, aes(class, hwy, fill= class)) +
geom_boxplot() +
geom_signif(comparisons = list(c("compact", "pickup"),
c("subcompact", "suv")),
map_signif_level = T)
ggplot(mpg, aes(class, hwy, fill=class)) +
geom_boxplot() +
geom_signif(comparisons = list(c("compact", "pickup"),
c("subcompact", "suv")),
map_signif_level=function(p)sprintf("p = %.2g", p))
ggplot(mpg, aes(class, hwy, fill= class)) +
geom_boxplot() +
geom_signif(annotations = c("First", "Second"),
y_position = c(30, 40), xmin=c(4,1), xmax=c(5,3))
据此,将文章R绘图应用实例:成组的t检验及绘图添加显著性做适当修改,参数如下:
ggplot(result_long_count2, aes(x=variable, y=value, fill=Treated)) +
geom_bar(position=position_dodge(.9), stat="identity",
colour="black",size=.3) +
geom_errorbar(aes(ymin=value-se, ymax=value+se),
size=.3, width=.2,
position=position_dodge(.9)) +
geom_signif(annotations = c("**", "*"), y_position = c(1.15,1.25),
xmin = c(0.78, 1.78),xmax = c(1.23, 2.23),
tip_length = c(c(0.05, 0.8),c(0.05, 0.55)), vjust = 0) +
scale_color_brewer("Set1") +
xlab("RNAi Treated") +
ylab("Relative Expression") +
ggtitle("The Effect of RNAi on two genes")
参考资料:
- R ggplot和ggsignif箱线图添加显著性差异标识
- 神奇小工具丨ggplot绘图显著性添加工具---ggsignif