ggplot2:用geom_bar表示平均值 [英] ggplot2 : Plot mean with geom_bar

查看:493
本文介绍了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),代表(1,2),代表(2,2)))

我绘制每个组的每个标签的条形图:

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


I have the following data frame:

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")

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.

Thanks for any help.

解决方案

simply use stat = "summary" and 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天全站免登陆