[英]ggplot2 : Plot mean with 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)))
我正在使用以下方法繪制每組每個標簽的條形圖:
ggplot(test2, aes(label, X2, fill=as.factor(groups))) +
geom_bar(position="dodge", stat="identity")
但是,我似乎無法找到stat="mean"
以便我可以在每個條形圖上繪制均值而不是標識。
謝謝你的幫助。
嘗試使用ggpubr 。 它創建類似 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")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.