2015-07-10 121 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