之前已經(jīng)完成了這個圖的繪圖交胚,但是排序是跟原文不一致的,這個時候就需要參考數(shù)據(jù)框列的順序錯亂音念,如何重排
第一步 找到原來的繪圖數(shù)據(jù)
rm(list = ls())
options(stringsAsFactors = F)
a=read.table('~/Downloads/biotrainee--2019-9-14/10-12 tree/RNA/counts/all.counts.id.txt',header = T)
dim(a)
cg=a[a[,1]=='pho',7:16]
library(ggpubr)
library(stringr)
dat=data.frame(gene=as.numeric(cg),
sample=names(cg),
group=str_split(names(cg),'_',simplify = T)[,1]
)
dat
ggbarplot(dat,x='sample',y='gene',
color = 'group',fill = 'group')
p <- ggbarplot(dat,x='sample',y='gene',
color = 'group',fill = 'group')
pp <- p + theme(axis.title.x=element_text(face="italic"),
axis.text.x = element_text(angle=50,vjust = 0.5))
pp
原來的圖是這樣的
重排數(shù)據(jù)框之后
rm(list = ls())
options(stringsAsFactors = F)
a=read.table('~/Downloads/biotrainee--2019-9-14/10-12 tree/RNA/counts/all.counts.id.txt',header = T)
dim(a)
cg=a[a[,1]=='pho',7:16]
library(ggpubr)
library(stringr)
dat=data.frame(gene=as.numeric(cg),
sample=names(cg),
group=str_split(names(cg),'_',simplify = T)[,1]
)
colnames(dat)
group=str_split(names(cg),'_',simplify = T)[,1]
group
s=c("WT_1.bam","WT_2.bam","WT_3.bam","PhoKO_1.bam","PhoKO_2.bam","PhoKO_3.bam","SppsKO_1.bam","SppsKO_2.bam","SppsKO_3.bam","SppsKO_4.bam")
df=as.data.frame(t(dat))
colnames(df)=as.character(dat[,2])
sy=match(s,colnames(df))
df2=df[,sy]
df2
dat=as.data.frame(t(df2))
dat$sampleid=rownames(dat)
library(ggpubr)
p <- ggbarplot(dat,x='sampleid',y='gene',
color = 'group',fill = 'group')
pp <- p + theme(axis.title.x=element_text(face="italic"),
axis.text.x = element_text(angle=50,vjust = 0.5))
pp
得到的圖如下所示:
總之重排就是建立索引站刑,按照需要排序的項目構(gòu)建,樣本少的時候就這樣手打出正確的排序用match函數(shù)得到索引宪睹,再按索引排序即可。這里注意蚕钦,行名不能重復(fù)亭病,列名可以重復(fù),其實沒有必要轉(zhuǎn)置的冠桃,但是因為我開始做錯了(開始我想按group的分組來完成命贴,結(jié)果是重復(fù)命名然后轉(zhuǎn)置道宅,嗯食听,主要還是R語言不熟練),后面發(fā)現(xiàn)錯了污茵,但是后面有些東西改了樱报,其他的東西也得改,因為懶就沒改泞当,將就著看吧迹蛤,追求完美的可以自行改掉。