R ggplot2:使用几何图形条绘制平均值
我有以下数据框:R ggplot2:使用几何图形条绘制平均值,r,ggplot2,bar-chart,visualization,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))) 但是,我似乎无法找到stat=“mean”,因此我可以在每个条形图上绘制平均值,而不是标识 感谢您的帮助。ggplot2喜欢1个数据点对应1个绘图点。使用摘要统计信息
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)))
但是,我似乎无法找到stat=“mean”
,因此我可以在每个条形图上绘制平均值,而不是标识
感谢您的帮助。
ggplot2
喜欢1个数据点对应1个绘图点。使用摘要统计信息创建新的数据框,然后使用stat=“identity”
require(重塑2)
plot.data只需使用stat=“summary”
和fun.y=“mean”
尝试使用。它创建类似于ggplot2的图表
library(ggpubr)
ggbarplot(test2, x = "label", y = "X2",
add = "mean", fill = "groups")
或者,添加一个方面:
ggbarplot(test2, x = "label", y = "X2",
add = "mean", fill = "groups",
facet.by = "groups")
本教程很好地介绍了如何实现这一点:这是一款非常棒、非常优雅的魔兽世界。谢谢你知道如何以一种好的方式获得较深的颜色吗?你如何修改它,在条的顶部添加标签?用ggplot 3.3.2
尝试过这一点,似乎fun.y
已经被弃用了。改用fun=“mean”
。
ggplot(test2) +
geom_bar(aes(label, X2, fill = as.factor(groups)),
position = "dodge", stat = "summary", fun.y = "mean")
library(ggpubr)
ggbarplot(test2, x = "label", y = "X2",
add = "mean", fill = "groups")
ggbarplot(test2, x = "label", y = "X2",
add = "mean", fill = "groups",
facet.by = "groups")