如何使用ggplot2标记图形的值的均值 [英] How to label graph with the mean of the values using ggplot2

查看:160
本文介绍了如何使用ggplot2标记图形的值的均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 geom_point 图,其中y轴点是各个x轴值的平均值。当我试图用平均值标出点时,我得到的就是所有的值。

I have created a geom_point graph where the y axis points are the mean of the values respective to each x-axis value. When I try to label the point with the mean, what I get is all the values.

这是我到目前为止:

This is what I have so far:

ggplot(test, aes(x=reorder(Type, Rating, mean), y=Rating, label=Rating)) +
       stat_summary(fun.y="mean", geom="point") +
       geom_text()


推荐答案

您可以像这样结合stat_summary和geom_text:

you can combine stat_summary and geom_text like this:

d <- data.frame(grp=gl(3,5, labels=letters[1:3]), v=rnorm(15))
ggplot(d, aes(grp, v)) + 
  stat_summary(fun.y=mean, geom="point") +
  stat_summary(aes(label=..y..), fun.y=mean, geom="text", size=8)

但可能最好事先汇总并格式化标签:

but probably it is better to aggregate beforehand and format the label:

ggplot(transform(ddply(d, .(grp), summarize, v=mean(v)), V=sprintf("%.02f", v)), 
  aes(grp, v)) +
  geom_point() + geom_text(aes(label=V))

这篇关于如何使用ggplot2标记图形的值的均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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