添加图例以指示形状 [英] Add legend to indicate shapes

查看:71
本文介绍了添加图例以指示形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要为添加图例中使用的形状的图例提供一些帮助,如下所述.该图如下所示:其为箱形图,均值为点,置信区间为误差线.

Need some help with adding legend for shapes used in the plot as described below. The plot is as below - its a box plot, points for means, error bars for confidence interval.

生成的图如下-如何在其中添加图例以告知red circles表示meangreen error bars表示confidence interval?-就像下面的图片一样

The resulting plot is as below - how do I add a legend to this so as to tell that the red circles indicate the mean and the green error bars indicate confidence interval ? - like in the image below

必填图例

情节

下面给出了用于生成上述代码的数据和代码,以供参考.

The data and code used to generate the above is given below for reference.

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

需要在上面的代码中添加哪些内容,以便获得图例以显示圆形和误差线形状指示的统计摘要?

What needs to be added to the above code so as to get the legend that reveal the stat summary that the circle and error bar shapes indicate?

推荐答案

如果您真的想分别给它们上色,则可以使用此代码.我正在使用geom_linerange而不是geom_errorbar在图例中获得一条垂直线.另外,如建议的那样,我在aes内映射颜色以获得图例,然后使用override.aes来限制每个值的绘图.

If you really want to color them separately, you can use this code. I am using geom_linerange instead of geom_errorbar to get a vertical line in the legend. In addition, as suggested, I am mapping colors inside of aes to get the legend, and then I am using override.aes to limit what plots for each of the values.

ggplot(data=df, aes(x=group, y=metric ) ) +
  geom_boxplot() +
  geom_point(data=metric_means
             , aes(x=metric_means[,1]
                   , y=metric_means[,2]
                   , colour = "Mean")
             , shape=21, size=2) +
  geom_linerange(data=metric_mean_ci
                 , aes(ymin=mean-ci
                      , ymax=mean+ci
                      , x=group
                      , y=mean
                      , color="95% CI")
                ) +
  scale_color_manual(name = "", values = c("green", "red")) +
  guides(colour = guide_legend(override.aes = list(linetype = c("solid", "blank")
                                                   , shape = c(NA, 1))))

赠予:

另一种需要较少复杂设置的方法是使用一些已经可用的功能,特别是stat_summary:

An alternative, which would require less complicated set up, is to use some of the functions already available to you, specifically, stat_summary:

ggplot(data=df
       , aes(x=group, y=metric ) ) +
  geom_boxplot() +
  stat_summary(
    aes(color = "Mean and 95% CI")
    , fun.data = mean_cl_normal
    )

赠予:

这篇关于添加图例以指示形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆