我正在尝试创建一个直方图,其中 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
)
关于r - 每个 bin 的均值直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13408503/