之前已经完成了这个图的绘图,但是排序是跟原文不一致的,这个时候就需要参考数据框列的顺序错乱,如何重排
第一步 找到原来的绘图数据
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
原来的图是这样的
重排数据框之后
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
得到的图如下所示:
总之重排就是建立索引,按照需要排序的项目构建,样本少的时候就这样手打出正确的排序用match函数得到索引,再按索引排序即可。这里注意,行名不能重复,列名可以重复,其实没有必要转置的,但是因为我开始做错了(开始我想按group的分组来完成,结果是重复命名然后转置,嗯,主要还是R语言不熟练),后面发现错了,但是后面有些东西改了,其他的东西也得改,因为懒就没改,将就着看吧,追求完美的可以自行改掉。