用geom_bar和stat ="identity"在平均值上绘制hline. [英] Plot hline at mean with geom_bar and stat="identity"
问题描述
我有一个条形图,其中确切的条形高度在数据框中.
I have a barplot where the exact bar heights are in the dataframe.
df <- data.frame(x=LETTERS[1:6], y=c(1:6, 1:6 + 1), g=rep(x = c("a", "b"), each=6))
ggplot(df, aes(x=x, y=y, fill=g, group=g)) +
geom_bar(stat="identity", position="dodge")
现在,我想添加两个线,以显示每个组中所有条形的平均值.我所拥有的
Now I want to add two hlines displaying the mean of all bars per group. All I get with
ggplot(df, aes(x=x, y=y, fill=g, group=g)) +
geom_bar(stat="identity", position="dodge") +
stat_summary(fun.y=mean, aes(yintercept=..y.., group=g), geom="hline")
是
由于我也想对任意数量的组执行此操作,因此,我希望仅使用ggplot解决方案.
As I want to do this for a arbitrary number of groups as well, I would appreciate a solution with ggplot only.
我想避免这样的解决方案,因为它不完全依赖传递给ggplot的数据集,具有冗余代码,并且在组数方面不灵活:
I want to avoid a solution like this, because it does not rely purely on the dataset passed to ggplot, has redundant code and is not flexible in the number of groups:
ggplot(df, aes(x=x, y=y, fill=g, group=g)) +
geom_bar(stat="identity", position="dodge") +
geom_hline(yintercept=mean(df$y[df$g=="a"]), col="red") +
geom_hline(yintercept=mean(df$y[df$g=="b"]), col="green")
提前谢谢!
- 添加了数据集
- 评论结果代码
- 更改了数据和图以澄清问题
推荐答案
如果我正确理解了您的问题,那么第一种方法就差不多了:
If I understand your question correctly, your first approach is almost there:
ggplot(df, aes(x = x, y = y, fill = g, group = g)) +
geom_col(position="dodge") + # geom_col is equivalent to geom_bar(stat = "identity")
stat_summary(fun.y = mean, aes(x = 1, yintercept = ..y.., group = g), geom = "hline")
根据stat_summary
的帮助文件:
stat_summary
在唯一的x上操作; ...
stat_summary
operates on unique x; ...
在这种情况下,stat_summary
默认继承了x = x
和group = g
的顶级美学映射,因此它将计算每个x的平均y值 中的每个g值,导致出现许多水平线.在stat_summary
的映射中添加x = 1
会覆盖x = x
(同时保留group = g
),因此对于每个g值,我们得到一个均值y值.
In this case, stat_summary
has inherited the top level aesthetic mappings of x = x
and group = g
by default, so it would calculate the mean y value at each x for each value of g, resulting in a lot of horizontal lines. Adding x = 1
to stat_summary
's mapping overrides x = x
(while retaining group = g
), so we get a single mean y value for each value of g instead.
这篇关于用geom_bar和stat ="identity"在平均值上绘制hline.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!