背景介绍
昨天我们讲了如何进行单因素方差分析或者非参数检验,以及组间的两两比较。
今天我们来看一下,对于数据如何作图,并且加上误差条以及显著性标记。
软件介绍
R、RStudio
作图教程
数据符合正态分布
1.在数据符合正态的条件下,适合做均值加减标准差或标准误的柱状图,首先,我们计算各个组别的均值和标准差或标准误。在mean()和sd()函数中,我们对Time进行计算均值和标准差,使用na.rm=TRUE,是由于我们的数据中包含了空值。(%>%就是一种管道符,传递函数用的)
# 计算均值和标准差library(tidyverse)T2plot <- T2%>% group_by(Group)%>% summarise( n=n(), mean=mean(Time,na.rm=TRUE), sd=sd(Time,na.rm = TRUE))T2plot
2.接下来我们进行数据绘图,然后我们基础的图就出来了
# 开始绘图ggplot(T2plot,aes(Group,mean,fill=Group))+ geom_col()
3.继续上面的,我们给柱子加上误差线,并设置宽度和粗细
# 开始绘图ggplot(T2plot,aes(Group,mean,fill=Group))+ geom_col()+ geom_errorbar(aes(Group, ymin=mean-sd, ymax=mean+sd, color=Group),width=.6,size=1)
4.在上述基础上,我们可以对x和y坐标进行名字的更改,以及对y轴范围进行更改
# 开始绘图ggplot(T2plot,aes(Group,mean,fill=Group))+ geom_col()+ geom_errorbar(aes(Group, ymin=mean-sd, ymax=mean+sd, color=Group),width=.6,size=1)+ xlab("Experimental group")+ ylab("Time")+ ylim(0,150)
5.接下来,我们对横纵坐标的字体大小进行更改:axis.title=element_text()是对x和y的标题名称进行更改;axis.text=element_text()是对x和y的轴的字体进行调整。同时,我们使用legend.position=”none”对标签去除
# 开始绘图ggplot(T2plot,aes(Group,mean,fill=Group))+ geom_col()+ geom_errorbar(aes(Group, ymin=mean-sd, ymax=mean+sd, color=Group),width=.6,size=1)+ xlab("Experimental group")+ ylab("Time")+ ylim(0,150)+ theme(legend.position ="none", axis.title = element_text(size=15), axis.text = element_text(size=15))
6.使用annotate()函数,对柱子增加标签,然后需要增加几个就复制几个,更改一下x和y的位置就行
# 开始绘图ggplot(T2plot,aes(Group,mean,fill=Group))+ geom_col()+ geom_errorbar(aes(Group, ymin=mean-sd, ymax=mean+sd, color=Group),width=.6,size=1)+ xlab("Experimental group")+ ylab("Time")+ ylim(0,150)+ theme(legend.position ="none", axis.title = element_text(size=15), axis.text = element_text(size=15))+ annotate("text",x=2,y=128,label="**",size=10)+ annotate("text",x=3,y=98,label="**",size=10)+ annotate("text",x=5,y=103,label="**",size=10)+ annotate("text",x=7,y=98,label="*",size=10)+ annotate("text",x=8,y=108,label="**",size=10)+ annotate("text",x=9,y=100,label="**",size=10)+ annotate("text",x=10,y=102,label="**",size=10)+ annotate("text",x=11,y=78,label="**",size=10)
7.如果你想给外围增加一个线框和填充图里面的颜色怎么办?使用panel.border=element_rect()以及panel.background=element_rect()
# 开始绘图ggplot(T2plot,aes(Group,mean,fill=Group))+ geom_col()+ geom_errorbar(aes(Group, ymin=mean-sd, ymax=mean+sd, color=Group),width=.6,size=1)+ xlab("Experimental group")+ ylab("Time")+ ylim(0,150)+ theme(legend.position ="none", axis.title = element_text(size=15), axis.text = element_text(size=15))+ annotate("text",x=2,y=128,label="**",size=10)+ annotate("text",x=3,y=98,label="**",size=10)+ annotate("text",x=5,y=103,label="**",size=10)+ annotate("text",x=7,y=98,label="*",size=10)+ annotate("text",x=8,y=108,label="**",size=10)+ annotate("text",x=9,y=100,label="**",size=10)+ annotate("text",x=10,y=102,label="**",size=10)+ annotate("text",x=11,y=78,label="**",size=10)+ theme(panel.border = element_rect(fill=NA,color = "black",size = 2), panel.background = element_rect(fill = "lightblue"))
8.最终图片如何导出呢,我们首先将图片拉到一个合适的大小,通过拉动边框,将图片适当展示
9.然后,使用ggsave()进行导出图片,直接在作图函数后面输入函数,命名图片以及图片格式,写入DPI是多少,即可导出到R的文件夹。
ggsave("不动时间图.png",dpi=600)
10.其他格式的图片,只需要更换图片格式尾缀即可,如“不动时间.pdf”,“不动时间.jpg”等。好了,今天的教程就这么多了,赶紧来学学吧!
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!