R ggplot2多因素连续条形图

R ggplot2多因素连续条形图,r,ggplot2,bar-chart,continuous,R,Ggplot2,Bar Chart,Continuous,我将使用ggplot2软件包附带的标准菱形数据集来说明我要查找的内容 我想建立一个如下图: library(ggplot2) ggplot(diamonds, aes(clarity, fill=cut)) + geom_bar(position="dodge") 但是,我想返回连续变量的平均值,而不是计数。我想返回切割和颜色,并得到平均克拉。如果我输入以下代码: ggplot(diamonds, aes(carat, fill=cut)) + geom_bar(position="dodge

我将使用ggplot2软件包附带的标准菱形数据集来说明我要查找的内容

我想建立一个如下图:

library(ggplot2)
ggplot(diamonds, aes(clarity, fill=cut)) + geom_bar(position="dodge")
但是,我想返回连续变量的平均值,而不是计数。我想返回切割和颜色,并得到平均克拉。如果我输入以下代码:

ggplot(diamonds, aes(carat, fill=cut)) + geom_bar(position="dodge")
我的输出是克拉数与切割数的比较


有人知道怎么做吗?

你可以得到一个新的数据框,其中
平均值(克拉)
切割
颜色
分组,然后绘制:

library(plyr)
data <- ddply(diamonds, .(cut, color), summarise, mean_carat = mean(carat))
ggplot(data, aes(color, mean_carat,fill=cut))+geom_bar(stat="identity", position="dodge")
带有
数据。表

library(data.table)
data <- data.table(diamonds)[,list(mean_carat=mean(carat)), by=c('cut', 'color')]
库(data.table)

数据太棒了!我已经使用data.table对其余文档进行了编码,所以我想知道是否也有data.table解决方案。我还使用了大量的数据集,因此速度优势得到了注意。是的,你可以同时使用
dplyr
data进行操作。table
plyr
更快,我将发布解决方案!我太爱你了,我可以吻你。在一个小时内,你解决了我上个星期辛苦解决的问题和3个不同的包!
library(data.table)
data <- data.table(diamonds)[,list(mean_carat=mean(carat)), by=c('cut', 'color')]