R - 排序从数据帧生成的条形图/直方图

时间:2014-06-16 15:42:34

标签: r plot histogram r-factor

如何更改从数据框生成的条形图的条形顺序?

我从这里尝试了代码: R - ordering in boxplot 但它似乎只适用于箱形图,例如

foo=data.frame(a=c(1,2,3),b=c("a","b","c"))
barplot(height=foo$a,names.arg=foo$b)
boxplot(foo$a~foo$b)
foo$c=factor(foo$b,c("c","b","a"))
barplot(height=foo$a,names.arg=foo$c)
boxplot(foo$a~foo$c)

1 个答案:

答案 0 :(得分:2)

这对我有用

foo$c=factor(foo$b, levels = c("c","b","a"))
foo <- foo[order(foo$c), ]
barplot(height=foo$a,names.arg=foo$c)