我能够创建一个stat_summary_bin,每个able (一个连续变量)显示y的avg值,如下所示。如何添加y值标签,显示每个bin以上的avg值?
ggplot(diamonds, aes(x=price, y=carat)) +
stat_summary_bin(fun.y = "mean",
geom="bar",
binwidth=5000
)

答案here使用stat_summary()不能解决我的问题。当我在那里尝试解决方案时,它没有正确地处理绑定宽度。
ggplot(diamonds,
aes(x=price, y=carat, label=round(..y..,2))
) +
stat_summary_bin(fun = "mean",geom="bar", binwidth=5000) +
stat_summary(fun = "mean",geom="text",binwidth=5000)

发布于 2020-07-07 05:52:14
stat_summary的相同解决方案适用于stat_summary_bin
ggplot(diamonds, aes(x=price, y=carat, label=round(..y..,2))) +
stat_summary_bin(fun = "mean",geom="bar", binwidth=5000) +
stat_summary_bin(fun = "mean",geom="text",binwidth=5000, vjust=-0.5)

用ggplot2_3.3.2测试。请注意,不推荐使用fun.y,并且帮助页鼓励您使用fun。
https://stackoverflow.com/questions/62768827
复制相似问题