ggplot堆栈条排序失败,未知函数desc

时间:2014-10-14 15:01:10

标签: r ggplot2 geom-bar

我试图更改堆积条形图中的级别顺序(它填充填充级别的顺序)。在ggplot文档中,它表明这是直截了当的:

# Use the order aesthetic to change stacking order of bar charts
w <- ggplot(diamonds, aes(clarity, fill = cut))
w + geom_bar()
w + geom_bar(aes(order = desc(cut)))

这似乎是我需要的,但当我尝试运行上面的代码时,它产生了这个:

  

eval中的错误(expr,envir,enclos):找不到函数&#34; desc&#34;

我需要包含另一个包来获取此功能吗?或者这是一个现在过时的方法来替换它?我尝试重新排序data.frame中的因子,但这并没有改变geom_bar如何堆叠它们。

我在(在RStudio中)查看的文档是用于[包ggplot2版本1.0.0索引]&#39;

感谢

2 个答案:

答案 0 :(得分:4)

desc()由plyr包提供,它是ggplot2的依赖项,因此您应该安装它。在生成绘图之前,只需使用library(plyr)加载它。

答案 1 :(得分:0)

此代码有效:

library(ggplot2)
library(dplyr)

w <- ggplot(diamonds, aes(clarity, fill = cut))
w + geom_bar()
w + geom_bar(aes(order = desc(cut)))