8

来自在线条形图指南

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

使用 <code>ggplot2</code> 的 <code>qplot</code> 功能的条形图

我如何让 5 个坐在底部,4 个在上面,3 个在上面?

4

3 回答 3

5

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

于 2010-03-13T00:31:38.407 回答
5
qplot(factor(cyl), data=mtcars, geom='bar', fill=factor(gear, level=5:3))
于 2010-03-12T02:49:33.657 回答
1

为了概括@xiechao 的解决方案(@hadley 在最新的 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()

在此处输入图像描述

于 2016-07-17T16:36:00.160 回答