R:排序条形图颜色

时间:2011-11-23 23:22:35

标签: r plot

我有一个生成条形图的数据矩阵,如:

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 个答案:

答案 0 :(得分: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

因此,当该类别不为零时,蓝色部分将位于底部。