条形图代码

先放一张成品图


使用软件:Rstudio

setwd("D:\\R_base\\Rstudio_workfile")  #设置工作路径

getwd()  #查看工作路径

A <- read.delim("Shannon.txt", header = T) #导入数据,比较常用txt格式

#载入R包  没有的话可安装 install. Packages("ggplot2")

       library(ggplot2)  

       library(forcats)

A$condictions <- as.factor(A$condictions)

A$condictions <- fct_inorder(A$condictions)

A$group <- as.factor(A$group)

A$group <- fct_inorder(A$group)

ggplot(A, aes(fill=condictions, y=value, x=group))+

           geom_bar(position=position_dodge(1),stat="summary",width=0.7,colour = "black", size=1)+

           theme_classic(base_size = 12)+

           geom_vline(aes(xintercept=as.numeric(as.factor(group))+0.5),linetype=2,cex=1.2)+

           geom_rect(aes(xmin=as.numeric(as.factor(group))+0.5,

                     xmax=Inf, ymin=(-Inf),ymax=Inf),

                     fill='grey90',color='grey90')+

           geom_vline(xintercept =A$condictions,linetype=2,cex=1.2)+

           geom_bar(position=position_dodge(1),stat="summary",width=0.7,colour = "black",size=1)+

           stat_summary(fun.data = 'mean_se', geom = "errorbar", colour = "black",

                      width = 0.2,position = position_dodge(1))+

           theme(legend.direction  = "horizontal", legend.position = "top")+

           labs(title = "", y="Shannon", x = "")+

           scale_y_continuous(limits = c(0,15),expand = c(0,0))+

           theme(axis.text.x = element_text(size = 12))+

           theme(axis.text.y = element_text(size = 12))+

           theme(axis.title = element_text(size = 14))+

 #添加数据点

           geom_jitter(data = A, aes(y = value),size = 3, shape = 21,

                      stroke = 0.01, show.legend = FALSE,

                      position = position_jitterdodge(jitter.height=1,dodge.width = 1))+

#标记显著性

          #geom_signif(y_position=c(5000,5000,5000,5500), xmin=c(0.6,1.1,1.6,1.9), xmax=c(0.8,1.3,1.8,2.3),

                  #annotation=c("**","**","****","****"), tip_length=0, size=0.8, textsize = 7,

                     #vjust = 0.3)+

          scale_fill_manual(values = c('#6E7ACA','#f9cc52','#0d898a','#e18283'))  #填充颜色


###导入文件格式如下

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
禁止转载,如需转载请通过简信或评论联系作者。