需要一些帮助来为图中使用的形状添加图例,如下所述。该图如下 - 它是一个箱形图,表示平均值的点,表示置信区间的误差线。
结果图如下 -我如何添加一个图例来告诉red circles
表明mean
和green error bars
表明confidence interval
?- 如下图所示
必填图例
Plot
![Box plot with mean & ci](https://i.stack.imgur.com/lNPON.jpg)
下面给出了用于生成上述内容的数据和代码,以供参考。
df <- data.frame(cbind(mtcars[,1], mtcars[,2])) #mtcars[, 1:2]
colnames(df) <- c("metric", "group")
df$group <- factor(df$group)
p1 <- ggplot(data=df, aes(x=group, y=metric ) ) +
geom_boxplot()
metric_means <- aggregate(df$metric, list(df$group), mean)
metric_ci_95 <- aggregate(df$metric, list(df$group), function(x){1.96*sd(x)/sqrt(length(x))})
metric_mean_ci = data.frame(group=metric_means[,1],mean=metric_means[,2], ci=metric_ci_95[,2])
# plot mean
p1 <- p1 + geom_point(data=metric_means, aes(x=metric_means[,1], y=metric_means[,2]),
colour="red", shape=21, size=2)
#plot confidence interval
p1 <- p1 + geom_errorbar(data=metric_mean_ci, aes(ymin=mean-ci, ymax=mean+ci, x=group, y=mean),
color="green", width=.1)
p1
需要在上面的代码中添加什么才能获得显示圆形和错误条形状指示的统计摘要的图例?