r - ggplot2:用geom_bar绘制平均值

标签 r ggplot2 bar-chart visualization

我有以下数据框:

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

我正在使用以下方法绘制每组每个标签的条形图:
ggplot(test2, aes(label, X2, fill=as.factor(groups))) + 
    geom_bar(position="dodge", stat="identity")

enter image description here

但是,我似乎无法找到stat="mean",因此可以在每个条形图上绘制均值,而不是标识。

谢谢你的帮助。

最佳答案

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

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

enter image description here

关于r - ggplot2:用geom_bar绘制平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30183199/

相关文章:

r - 如何根据数据框中的分组来增加数字距离?

python - python 中的 ggplot : plot size and color

r - crosstalk的filter_select不会过滤图表

r - 如何使用 ggplot2 获取具有不同几何匹配的 2 个图形的网格

reactjs - Recharts:条形图中每个条形的不同梯度

r - ggplot2 没有按字母顺序排列条形图,而它应该这样做?

r - 从杂乱的字符列表到 R 中的矩阵

r - 可以获得非标准评估以在 dplyr 中为 filter_ 和 count_ 但不是 distinct_ 工作

r - 将重复项移动到新列中

r - ggplot中轴的默认文本颜色