如何更改从数据框生成的条形图的条形顺序?
我从这里尝试了代码: 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)
答案 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)