在R中使用ggplot2绘制子组箱形图并同时显示两组的平均值,可以按照以下步骤进行操作:
install.packages("ggplot2")
library(ggplot2)
data <- data.frame(Group = c(rep("Group 1", 100), rep("Group 2", 100)),
Value = c(rnorm(100, mean = 0, sd = 1), rnorm(100, mean = 2, sd = 1)))
p <- ggplot(data, aes(x = Group, y = Value))
p <- p + geom_boxplot(fill = "lightblue", color = "black")
p <- p + stat_summary(fun = mean, geom = "point", shape = 23, size = 3, fill = "red", color = "black")
p <- p + labs(title = "Boxplot with Mean", x = "Group", y = "Value")
print(p)
这样就可以在子组箱形图中绘制两组数据的平均值。根据具体的需求,可以进一步调整图表的样式和布局。
领取专属 10元无门槛券
手把手带您无忧上云