ggplot2 : 用 geom_bar 绘制均值 [英] ggplot2 : Plot mean with geom_bar

查看:56
本文介绍了ggplot2 : 用 geom_bar 绘制均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据框:

test2 <- data.frame(groups = c(rep("group1",4), rep("group2",4)), 
    X2 = c(rnorm(4), rnorm(4)) , 
    label = c(rep(1,2),rep(2,2),rep(1,2),rep(2,2)))

我正在使用以下方法绘制每组每个标签的条形图:

and I am plotting the bar graphs for each label per group using:

ggplot(test2, aes(label, X2, fill=as.factor(groups))) + 
    geom_bar(position="dodge", stat="identity")

但是,我似乎无法找到 stat="mean" 所以我可以在每个条形图上绘制均值而不是身份.

However, I am cannot seem to be able to find a stat="mean" so I can plot the means on each bar graph instead of the identity.

感谢您的帮助.

推荐答案

只需使用 stat = "summary"fun.y = "mean"

ggplot(test2) + 
  geom_bar(aes(label, X2, fill = as.factor(groups)), 
           position = "dodge", stat = "summary", fun.y = "mean")

这篇关于ggplot2 : 用 geom_bar 绘制均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆