1. 载入包
library(ggplot2)
library(ggsignif)
library(ggpubr)
library(reshape2)
library(ggsci)
2. 对一组数据画箱线图
### stat_signif {ggsignif}:Create significance layer
## wilcox(秩合检验)
ggplot(iris,aes(Species,Sepal.Length,fill=Species)) +
geom_boxplot()+geom_jitter(shape=16,size=2,position=position_jitter(0.2)) +
geom_signif(comparisons = list(c("versicolor", "virginica"),
c("versicolor","setosa"),
c("virginica","setosa")),
map_signif_level=T,
textsize=6,test=wilcox.test,step_increase=0.1) +
xlab(NULL) +
theme_classic()
## t_test分析
ggplot(iris,aes(Species,Sepal.Length,fill=Species)) +
geom_boxplot()+geom_jitter(shape=16,size=2,position=position_jitter(0.2)) +
geom_signif(comparisons = list(c("versicolor", "virginica"),
c("versicolor","setosa"),
c("virginica","setosa")),
map_signif_level=T,
textsize=6,test=t.test,step_increase=0.1) +
xlab(NULL) +
theme_classic()
###stat_compare_means {ggpubr}:Add mean comparison p-values to a ggplot,
#### such as box blots, dot plots and stripcharts.
## anova分析
ggplot(iris,aes(Species,Sepal.Length,fill=Species)) +
geom_boxplot()+
geom_jitter(shape=16,size=2,position=position_jitter(0.2))+
stat_compare_means(method = "anova")+theme_bw()
## Kruskal-Wallis分析
#克鲁斯卡尔-沃利斯检验(Kruskal-Wallis test)亦称“K-W检验”、“H检验”等。
#用以检验两个以上样本是否来自 同一个概率分布的一种非参数方法。
ggplot(iris,aes(Species,Sepal.Length,fill=Species)) +
geom_boxplot()+
geom_jitter(shape=16,size=2,position=position_jitter(0.2))+
stat_compare_means()+theme_bw()
3. 对数据多分面作图
#iris
#melt(iris) # id.vars:Species
iris %>% melt() %>%
ggplot(aes(variable,value,fill=Species)) + # 按种类作分面图
geom_boxplot() +
#geom_jitter(shape=16,size=0.5,position=position_jitter(0.05)) + #只能按variable画点
scale_fill_jco() +
#stat_compare_means(label = "p.format")+
stat_compare_means(label = "p.signif") +
facet_grid(.~variable,scales = "free",space="free_x") +
theme_bw()+theme(axis.text.x = element_blank(),
axis.title.x= element_text(size = rel(1.3)),
axis.title.y = element_text(size = rel(1.3), angle = 90))
#?facet_grid
# scale_color_jco {ggsci}