Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change the stacking order in a bar chart in ggplot2?

Tags:

r

ggplot2

From the online bar chart guide:

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

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

How do I get 5 to sit on the bottom, 4 above that, and 3 on top?

like image 307
Jeremy Leipzig Avatar asked Mar 11 '10 18:03

Jeremy Leipzig


People also ask

How do I change the order of a stacked bar chart?

Under Chart Tools, on the Design tab, in the Data group, click Select Data. In the Select Data Source dialog box, in the Legend Entries (Series) box, click the data series that you want to change the order of. Click the Move Up or Move Down arrows to move the data series to the position that you want.

How do I reorder bar charts in R?

To reorder bars manually, you have to pass stat=”identity” in the geom_bar() function.

How do I reorder Ggplot in R?

To reorder the boxplot we will use reorder() function of ggplot2. By default, ggplot2 orders the groups in alphabetical order. But for better visualization of data sometimes we need to reorder them in increasing and decreasing order. This is where the reorder() function comes into play.


2 Answers

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

like image 57
hadley Avatar answered Sep 30 '22 04:09

hadley


qplot(factor(cyl), data=mtcars, geom='bar', fill=factor(gear, level=5:3))
like image 35
xiechao Avatar answered Sep 30 '22 03:09

xiechao