Como altero a ordem de empilhamento em um gráfico de barras no GGPlot2?

StackOverflow https://stackoverflow.com/questions/2427742

  •  19-09-2019
  •  | 
  •  

Pergunta

De Guia de gráfico de barras online:

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

barplot using <code>qplot</code> feature of <code>ggplot2</code>

Como faço para que 5 se sentem no fundo, 4 acima disso e 3 no topo?

Foi útil?

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()

enter image description here

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top