2011-11-23 125 views
-8

我有数据矩阵生成barplot像:R:排序barplot颜色

enter image description here

我希望我的barblot是这样的:

enter image description here

在哪里正如我在评论中指出的那样,蓝色部分应该始终位于底部。这是我的R代码:

file<-read.csv(file="file.csv", sep="\t", header=T) 
    m<-table(file) 
    m 
     path 
    mir    cp eip gip mt os 
     aga-bantam  0 0 0 4 0 
     aga-let-7  0 0 0 2 5 
     aga-miR-1  0 0 0 3 0 
     aga-miR-10  0 11 9 2 0 
     aga-miR-100  4 0 0 0 0 
     aga-miR-1000 2 2 0 15 0 
     aga-miR-11  5 0 0 0 0 
     aga-miR-1174 4 0 0 10 0 
     aga-miR-1175 0 0 0 6 0 
     aga-miR-12  0 3 0 0 0 
     aga-miR-124  0 0 0 8 0 
     aga-miR-133  0 5 19 0 0 
     aga-miR-137  0 0 2 0 0 
     aga-miR-13b  0 0 9 0 0 
     aga-miR-184  0 0 0 9 2 
     aga-miR-1889 0 0 11 2 0 
     aga-miR-1890 0 0 5 11 0 
     aga-miR-1891 0 0 0 3 0 
     aga-miR-190  0 4 0 13 0 
     aga-miR-2  0 0 10 0 0 
     aga-miR-210  0 0 0 4 0 
     aga-miR-219  0 3 5 0 0 
     aga-miR-263  0 2 0 0 7 
     aga-miR-263b 0 3 0 16 4 
     aga-miR-275  0 4 0 7 2 
     aga-miR-276-3p 0 0 4 20 0 
     aga-miR-276-5p 2 0 0 4 0 
     aga-miR-277  0 0 0 20 0 
     aga-miR-278  0 0 0 3 0 
     aga-miR-279  0 5 0 0 0 
     aga-miR-281  6 5 4 5 0 
     aga-miR-282  4 2 15 4 0 
     aga-miR-283  0 0 7 1 0 
     aga-miR-305  0 3 5 10 0 
     aga-miR-307  0 0 0 4 0 
     aga-miR-308  0 0 0 0 2 
     aga-miR-309  0 0 0 9 0 
     aga-miR-315  0 0 0 13 0 
     aga-miR-317  0 0 0 4 2 
     aga-miR-34  0 3 0 3 0 
     aga-miR-375  0 0 0 2 2 
     aga-miR-7  0 0 0 10 0 
     aga-miR-79  0 0 0 5 0 
     aga-miR-8  0 0 3 2 0 
     aga-miR-87  5 2 4 0 0 
     aga-miR-927  0 0 11 0 0 
     aga-miR-929  0 0 0 3 0 
     aga-miR-92a  0 0 10 4 0 
     aga-miR-92b  0 2 9 0 0 
     aga-miR-957  0 0 0 8 0 
     aga-miR-965  5 0 0 10 0 
     aga-miR-970  0 0 7 5 0 
     aga-miR-981  0 4 0 4 0 
     aga-miR-989  0 0 0 8 0 
     aga-miR-993  0 2 0 3 0 
     aga-miR-996  0 0 3 2 0 
     aga-miR-9a  0 0 0 3 0 
     aga-miR-9b  5 0 0 2 0 
     aga-miR-9c  0 0 0 5 0 
     aga-miR-iab-4 5 0 0 0 0 

    barplot (t(m),col=rainbow(5),las=2) 
+1

是什么问题?根据你的例子,颜色总是按照正确的顺序。也许你的意思是说你希望你的数据与你的例子中使用的数据相同? – joran

+0

感谢您的回复,例如我需要将所有蓝色值都放在底部,因为您可以看到它有时出现在底部,有时出现在底部,有时出现在顶部。 – Mushal

+2

您的蓝色值只在底部没有绿色,黄色或红色时出现在底部。就像第二个例子中的绿色,其中两个在底部,因为它们下面没有红色。 –

回答

2

使用您添加的代码,我可以重现您的图形。请注意,某些柱状图不会包含蓝色区段,因为在您的数据中,该列中的某些值实际上为零。但是,我们可以通过重新排列数据的列和相关的色彩重新排列部分订单:

#You data was a table, but it was easier for me to copy+paste 
# it and then read it as a data.frame 
m <- read.table("~/Desktop/stackoverflowExamples/so.txt",header = TRUE,sep = "") 
#Move the first column over to be the rownames 
rownames(m) <- m[,1] 
m <- m[,-1] 

#Note that I just rearranged each piece so that the blue 
# segments will be first; this includes the color and 
# legend text ordering 
barplot(t(m[,c(4,1:3,5)]),col=rainbow(5)[c(4,1:3,5)], 
     las=2,cex.names = 0.5, 
     legend.text = colnames(m)[c(4,1:3,5)]) 

产生这样的:

enter image description here

,这样当该类别不为零,蓝色部分将位于底部。

+0

非常感谢 – Mushal