# Title : TODO
# Objective : TODO
# Created by: Administrator
# Created on: 2021/1/30
#导入包
library('tidyverse')
library('reshape2')
#定义向量
l <-c('Phylum','Order','Genus','Species')
h <- 'f://'
#定义函数
f<-function (h,m){
data <-readxl::read_xlsx(paste("F://",m,".xlsx",sep=''))#,sheet = 'Sheet2') #读取excel
b <- melt(data,variable.name ='Taxa') #把数据整理为 ggplot2能读取的格式
c <- b[[m]]
fil <-c('#FF0000','#008B00','#FFD700','#9400D3','#00BFFF','#C1CDCD','#66CDAA','#000080','#DB7093','#C6E2FF',
'#EEEE00','#9FB6CD','#00FF7F','#4682B4','#912CEE','#EEE5DE','#FFDAB9','#96CDCD','#9AFF9A','#FF8C69','#EEE8AA','#1C1C1C')#定义颜色
# stat数据随y,柱宽,width,cok柱边框颜色。position 柱状填充方式。
ggplot(b) +
geom_bar( mapping = aes(x = c, y=value, fill=Taxa,color=fil)
,stat = "identity", width=0.87, col='black',position='fill')+
facet_grid(.~ID,scales='free',space='free')+
theme_bw() +
theme(axis.text.x = element_text(face = "bold", vjust = 0.5, hjust = 0.5,angle = 45),text=element_text("serif"),legend.position = 'none') +
scale_fill_manual(values= fil)+
xlab(m)+
ylab('Relative abundance')
guides(fill = guide_legend(ncol=1))
ggsave(filename = paste(h,m,'.png',sep='') , height=8, width=8, units = 'in', dpi=300)
}
for (m in l){
f (h,m)
print(m)
}
ggoplot2绘制 柱状图
最新推荐文章于 2023-03-22 15:58:52 发布