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

问题描述 投票:4回答:2

我正在尝试更改堆积条形图中的级别顺序(它填充填充级别的顺序)。在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):找不到函数“desc”

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

我正在查看的文档(在RStudio中)是'[Package ggplot2 version 1.0.0 Index]'

谢谢

r ggplot2 geom-bar
2个回答
4
投票

desc()是由plyr包提供的,它是ggplot2的依赖,所以你应该安装它。在生成你的情节之前,只需用library(plyr)加载它。


0
投票

此代码有效:

library(ggplot2)
library(dplyr)

w <- ggplot(diamonds, aes(clarity, fill = cut))
w + geom_bar()
w + geom_bar(aes(order = desc(cut)))
© www.soinside.com 2019 - 2024. All rights reserved.