pos_df=do.call(rbind,lapply(1:10, function(i){
data.frame(gene=paste0('gene',i,LETTERS),
chr=sample(paste0('chr',1:22),26,replace = T),
start= sample(1:1000,26))
}))
pos_df=pos_df[with(pos_df,order(chr,start)),]
pos_df$chr=as.factor(pos_df$chr)
plot(pos_df$chr,pos_df$start,las=2)
首先我们的排序并没有按照染色体顺序,而是
> levels((pos_df$chr))
[1] "chr1" "chr10" "chr11" "chr12" "chr13" "chr14" "chr15" "chr16"
[9] "chr17" "chr18" "chr19" "chr2" "chr20" "chr21" "chr22" "chr3"
[17] "chr4" "chr5" "chr6" "chr7" "chr8" "chr9"
>
这种情况下sort这个向量其实是没有意义的, 有两个方案可以解决它!
首先是设置因子的水平即可