R 添加带有统计摘要(fun.y=";mean";)的连续缩放绘图条

R 添加带有统计摘要(fun.y=";mean";)的连续缩放绘图条,r,ggplot2,R,Ggplot2,我想创建一个条形图,按因子总结数据的平均值。然后我想让条的填充颜色反映平均值。我没办法让人工作。我认为这是与stat\u summary()有关的 使用diamonds数据集: p <- ggplot(data = diamonds, aes(x = cut, y = price, fill = price)) + stat_summary(fun.y = "mean", geom= "bar" ) p p以下是一种方法: p <

我想创建一个条形图,按因子总结数据的平均值。然后我想让条的填充颜色反映平均值。我没办法让人工作。我认为这是与
stat\u summary()
有关的

使用
diamonds
数据集:

p <- ggplot(data = diamonds,
                 aes(x = cut, y = price, fill = price)) + 
        stat_summary(fun.y = "mean", geom= "bar" )
p
p以下是一种方法:

p <- ggplot(data = diamonds,
            aes(x = cut, y = price)) + 
         stat_summary(fun.y = "mean", geom= "bar", aes(fill = ..y..)) 
您可以使用语法
.variable..
(如
.y..
)将此数据中定义的一些变量用作映射

head(ggplot_build(p)$data[[1]])
     fill x group ymin        y     ymax PANEL xmin xmax colour size linetype alpha
1 #4894D0 1     1    0 4358.758 4358.758     1 0.55 1.45     NA  0.5        1    NA
2 #2D5F89 2     2    0 3928.864 3928.864     1 1.55 2.45     NA  0.5        1    NA
3 #306592 3     3    0 3981.760 3981.760     1 2.55 3.45     NA  0.5        1    NA
4 #56B1F7 4     4    0 4584.258 4584.258     1 3.55 4.45     NA  0.5        1    NA
5 #132B43 5     5    0 3457.542 3457.542     1 4.55 5.45     NA  0.5        1    NA