2010-03-11 161 views

回答

5

qplot(factor(cyl), data=mtcars, geom="bar", fill=factor(gear), order = -gear)

+0

這給出eval中的錯誤(expr,envir,enclos):找不到對象'gear' – 2016-07-15 03:48:51

5
qplot(factor(cyl), data=mtcars, geom='bar', fill=factor(gear, level=5:3)) 
+1

你明白了,但是定義新的'data.frame'然後在'transform()''ed'上運行'qplot'會更方便,因此得到了乾淨的圖例和座標軸:'newone < - 變換(mtcars,cyl = factor(cyl),gear = factor(gear,levels = 5:3))'然後:'qplot(cyl,data = newone,fill = gear)'...而且代碼更清晰太! – aL3xa 2010-03-13 00:06:17

1

要概括@ xiechao的解決方案(@哈德利的不最新ggplot工作),你可以扭轉因素的訂單來實現:

library(ggplot2) 
data(mtcars) 
mtcars$gear <- factor(mtcars$gear) # First make factor with default levels 
mtcars$gear <- factor(mtcars$gear, levels=rev(levels(mtcars$gear))) 
qplot(cyl, data=mtcars, geom="bar", fill=gear) 
# Or with ggplot 
ggplot(mtcars, aes(factor(cyl), fill=gear)) + geom_bar() 

enter image description here

相關問題