2015-07-10 206 views
1

我想重新排列我的bar_plot從最高值到最低值。我的代碼有什麼問題?基於價值列重新排列geom_bar

head(summary.m) 
    Var1 Var2 value 
2 u-A Length 1155 
3 u-AA Length 422 
4 u-AAA Length 119 
5 u-AAC Length  6 
6 u-AAG Length 19 
7 u-AAT Length 49 

ggplot(summary.m, aes(x=reorder(Var1,-value),y=value)) + geom_bar(stat="identity") + 
    theme_bw(base_size=8) + 
    theme(axis.text.x = element_text(colour = "black",angle=90)) 
+1

這可能有助於:https://kohske.wordpress.com/2010/12/29/faq-how-to-order-the-factor-variables-in-ggplot2/ – mts

回答

2

你只需要指定在reorder中使用的函數。試試這個吧

ggplot(summary.m, aes(x=reorder(Var1,value, FUN=sum),y=value)) + geom_bar(stat="identity") + 
    theme_bw(base_size=8) + 
    theme(axis.text.x = element_text(colour = "black",angle=90)) 

注意:你的代碼適合我,所以我已經顛倒了順序。

+0

Summary.factor(c( 41L,38L,42L),na.rm = FALSE): 'sum'對於因素 – BioMan

+0

無意義'value'應該是一個數字。你應該檢查你的數據。 –

+0

我這樣做,它的工作原理:as.numeric(value) – BioMan