2014-09-04 144 views
55

我正在嘗試製作一個條形圖,其中圖表的排列順序爲miRNA,最高的爲valuemiRNA爲最低。爲什麼我的代碼不工作?geom_bar ggplot2中的條形碼重新排序

> head(corr.m) 

     miRNA   variable value 
1 mmu-miR-532-3p  pos  7 
2 mmu-miR-1983  pos 75 
3 mmu-miR-301a-3p  pos 70 
4 mmu-miR-96-5p  pos  5 
5 mmu-miR-139-5p  pos 10 
6 mmu-miR-5097  pos 47 

ggplot(corr.m, aes(x=reorder(miRNA, value), y=value, fill=variable)) + 
    geom_bar(stat="identity") 

回答

103

您的代碼工作正常,除了barplot從低到高排序。當你想訂購由高向低的吧,你將有value前添加-標誌:

ggplot(corr.m, aes(x = reorder(miRNA, -value), y = value, fill = variable)) + 
    geom_bar(stat = "identity") 

這給:

enter image description here

+0

它不會在我的情節訂購某些原因 – user3741035 2014-09-04 11:39:45

+0

@ user3741035奇怪。您是否在上面提供的示例數據集或整個數據集上使用它? – Jaap 2014-09-04 11:43:58

+0

整個數據集。編輯:沒有在樣品上工作 – user3741035 2014-09-04 11:44:36