ggplot2中汇总统计的图例

编程入门 行业动态 更新时间:2024-10-09 20:26:49
本文介绍了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中汇总统计的图例

本文发布于:2023-11-28 19:59:37,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图例

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!