ggplot2中汇总统计的图例 [英] legend for summary statistics in ggplot2

查看:183
本文介绍了ggplot2中汇总统计的图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是剧情的代码

Here is the code for the plot

library(ggplot2)
df <- data.frame(gp = factor(rep(letters[1:3], each = 10)), y = rnorm(30))
library(plyr)
ds <- ddply(df, .(gp), summarise, mean = mean(y), sd = sd(y))
ggplot(df, aes(x = gp, y = y)) +
   geom_point() +
   geom_point(data = ds, aes(y = mean), colour = 'red', size = 3)

我想拥有这种情节的图例可以识别数据值和平均值,就像这样

I want to have a legend for this plot that will identify the data values and mean values some thing like this

Black point = Data
Red point   = Mean.

任何获得理想结果的指针都将被高度赞赏。谢谢

Any pointer to get the desired result will be highly appreciated. Thanks

推荐答案

使用手动缩放,即在您的情况下 scale_colour_manual 。然后使用每个geom的 aes()函数将颜色映射到比例值:

Use a manual scale, i.e. in your case scale_colour_manual. Then map the colours to values in the scale using the aes() function of each geom:

ggplot(df, aes(x = gp, y = y)) +
  geom_point(aes(colour="data")) +
  geom_point(data = ds, aes(y = mean, colour = "mean"), size = 3) +
  scale_colour_manual("Legend", values=c("mean"="red", "data"="black"))

这篇关于ggplot2中汇总统计的图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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