2018-07-31之基迪奥论坛学习的柱状图加显著性

#读取数据
data<-read.table("1233.txt",sep="\t",header = TRUE)
data

#?ks.test
#?shapiro.test

#方差齐性检验
nom<-bartlett.test(dia~lab,data = data)
# 如果是2(多)因数,使用interaction()函数,leveneTest(y~interaction(var1,var2),data = data)
nom
nom$p.value

library("car")
nom1<-leveneTest(dia~lab,data = data)
# 如果是2(多)因数,使用*,leveneTest(y~ivar1*var2,data = data)
nom1
nom1$`Pr(>F)`


#方差分析
oneway<-aov(dia~lab,data = data)

anova(oneway)


#多重比较方法

#TukeyHSD法
#Tukey’s Honestly Significant Difference
bijiao<-TukeyHSD(oneway,ordered = FALSE,conf.level = 0.95)
bijiao

# LSD法
#Fisher’s Least Significant Difference
library("agricolae")
out <- LSD.test(oneway,"lab", p.adj="bonferroni")
out

# 整理用于作图的数据框
rowname<-row.names(out$means)
mean<-out$means[,1]
sd<-out$means[,2]
marker<-out$groups$groups

plotdata<-data.frame(rowname,mean,sd,marker)
plotdata
# ggplot2 绘制带显著性标记的柱状图

library("ggplot2")


p1<-ggplot(plotdata,aes(x=factor(rowname),y=mean))+geom_bar(position =position_dodge(0),fill="blue",width = 0.5,stat = "identity")
p1
p2<-p1+geom_errorbar(aes(ymin=mean-sd,ymax=mean+sd),position=position_dodge(0.6),width=0.2)

p3<-p2+geom_text(aes(x=factor(rowname),y=mean+sd+2.0,label=marker),size=4,position = position_dodge(0.6))
p3

p4<-p3+xlab("")+ylab("Lesion diameter (mm)")
p4

p5<-p4+coord_cartesian(ylim = c(0,60),expand = T)
p5

#更改y轴显示范围,这里的expand默认为TRUE。
mytheme<-theme_bw()+theme(axis.title = element_text(size = 12),
                          axis.text = element_text(size=12),
                          panel.grid.major = element_line(color = "white"),
                          panel.grid.minor = element_line(colour = "white"),
                          axis.text.x = element_text(size = 12,angle=45,vjust=0.6,hjust=0.8,color = "black"),
                          axis.text.y = element_text(size = 12,color = "black"),
                          legend.text = element_text(size = 12),legend.title = element_blank(),
                          plot.margin = unit(c(0.5,0.5,0.5,0.5), "cm"))
p5+mytheme
ggsave("wanyixia1.png", width = 10, height = 10, units = "cm")

image.png

搬运链接
数据和脚本下载链接

你可能感兴趣的:(2018-07-31之基迪奥论坛学习的柱状图加显著性)