從online bar chart guide:如何在ggplot2的條形圖中更改堆疊順序?
qplot(factor(cyl), data=mtcars, geom="bar", fill=factor(gear))
如何獲得5坐底,4上面,和3在上面?
從online bar chart guide:如何在ggplot2的條形圖中更改堆疊順序?
qplot(factor(cyl), data=mtcars, geom="bar", fill=factor(gear))
如何獲得5坐底,4上面,和3在上面?
qplot(factor(cyl), data=mtcars, geom="bar", fill=factor(gear), order = -gear)
這給出eval中的錯誤(expr,envir,enclos):找不到對象'gear' – 2016-07-15 03:48:51
qplot(factor(cyl), data=mtcars, geom='bar', fill=factor(gear, level=5:3))
你明白了,但是定義新的'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
要概括@ 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()
對於色盲的人來說這完全不友好! :-) – dalloliogm 2010-03-12 16:41:39
是否有任何專門爲這些用戶設計的配色方案?它會很好... – aL3xa 2010-03-12 19:28:28