Como altero a ordem de empilhamento em um gráfico de barras no GGPlot2?
Pergunta
De Guia de gráfico de barras online:
qplot(factor(cyl), data=mtcars, geom="bar", fill=factor(gear))
Como faço para que 5 se sentem no fundo, 4 acima disso e 3 no topo?
Solução
qplot(factor(cyl), data=mtcars, geom="bar", fill=factor(gear), order = -gear)
Outras dicas
qplot(factor(cyl), data=mtcars, geom='bar', fill=factor(gear, level=5:3))
Para generalizar a solução do @xiechao ( @hadley não funciona no último ggplot), você pode reverter as ordens fatoriais para conseguir isso:
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()
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow