數(shù)據(jù)用的是KEGG的L2級的分類相對豐度
長這個樣子,柱狀圖的x軸,第一列是命名,第二列是分類,第三列是相對豐度

安裝包就不解釋了,直接讀取,安裝包的過程可以看其他的,這幾個都是常規(guī)可以安裝的,不可以的話可以直接百度參考其他的安裝過程
#讀包
library(ggplot2)
library(reshape2)
library(carData)
library(car)
#讀取數(shù)據(jù)
a1 <-read.table("D:/Rrice/KEGG/Bacteria/L2 abundance.txt",header=T,sep = '\t')
#查看數(shù)據(jù)
head ( a1 )
#繪圖
p1<-ggplot(a1,aes(x=reorder(CategoryL2,abundance),y=abundance,fill=CategoryL1))+#確定x,y軸,x=reorder是用來讓柱狀圖按從小到大排列的,如果是多分組的會按照分組的和進行排序
? geom_col( width = 0.7,position = 'dodge') +#設置柱狀圖寬度
? scale_fill_manual(values =c ('#AE3321','#22AB33','#445299','#A233A3','#22B3A2','#A3B1C2'))+#添加分類顏色
? labs(title = NULL,x=NULL, y = "Number of Edge",fill = NULL)+#顯示標簽(xy軸和圖例)
? theme_bw()+#去掉背景色
? theme(legend.position = 'n')+#不顯示圖例
? theme(panel.grid.major=element_blank(),panel.grid.minor=element_blank())+
? coord_flip()+#x,y軸轉置
theme(strip.text.y = element_text(angle = 0))+
? facet_grid(CategoryL1 ~.,scales='free', space = 'free', margins ='am', drop = T)#分類別,按照我的數(shù)據(jù)的第二列進行分類
p1
#導出PDF圖片,默認圖片大小,可以利用width=?和height=?來設置圖片的寬和高
ggsave('D:/Rrice/KEGG/Bacteria/L2 abundance.pdf')
這個code中還可以加讓后面的分列自動換行的指令,我能力有限,大家加油!
上成品
