r - 每个 bin 的均值直方图

标签 r ggplot2

我正在尝试创建一个直方图,其中 bin 高度是落入每个 bin 的值的平均值。下面的 p2 代码是我认为可行的。

library(ggplot2)
m <- mtcars[1:20, ];
p <- ggplot(m, aes(x = mpg)) + geom_histogram(binwidth = 5);
p <- p + aes(weight = wt); # works, but is the sum of the wt
p2 <- p + aes(weight = wt / ..count..); # does not work, but the idea I am going for

对不起,如果我在这里遗漏了一些明显的东西,但我很感激你的帮助。

最佳答案

您现在可以使用 stat_summary_bin为了这。

ggplot(mtcars, aes(x=mpg, y=wt)) +
  stat_summary_bin(fun.y = "mean",
                   geom="bar",
                   binwidth=5
                   )
enter image description here

关于r - 每个 bin 的均值直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13408503/

相关文章:

r - 使用列名创建两列数据框

python - 让 Rpy2 与 rgdal 一起工作以获取空间子集点

r - 使用 ggplot2 从数据框中绘制多条线

r - 如何在 ggplot 中为一种美学创建多个图例?

r - ggplot中堆叠条的顺序

r - 如何将wordcloud放入grob中?

R - 舍入到最接近的一半

r - 对 ggplot 点使用自己的符号,更改图例符号以匹配

r - 在ggplot2中的图例下插入表格

r - ggplot 标签上的 curl curl