geom text with stat_summary

2,712 views
Skip to first unread message

adam.l...@pnc.com

unread,
May 9, 2014, 5:07:09 PM5/9/14
to "ggplot2@goog...@pnc.com
Hi,

Can you use stat_summary along with geom_text in order to label the position(s) of certain location?

For example, I expected the following code to label the point, (5,5) and no others, but it labels all of them.

Is there an easy way to do the same thing, without figuring out the location outside of ggplot2?

tst<-data.frame(x=1:5,y=1:5,z=c("a","b","c","d","e"))

ggplot(tst,aes(x,y))+
geom_point()+
stat_summary(fun.y="max",geom="text",aes(label=z))

Thanks for the help.



Adam Loveland


The contents of this email are the property of PNC. If it was not addressed to you, you have no legal right to read it. If you think you received it in error, please notify the sender. Do not forward or copy without permission of the sender. This message may contain an advertisement of a product or service and thus may constitute a commercial electronic mail message under US Law. The postal address for PNC is 249 Fifth Avenue, Pittsburgh, PA 15222. If you do not wish to receive any additional advertising or promotional messages from PNC at this e-mail address, click here to unsubscribe: http://pages.e.pnc.com/globalunsub/
By unsubscribing to this message, you will be unsubscribed from all advertising or promotional messages from PNC. Removing your e-mail address from this mailing list will not affect your subscription to alerts, e-newsletters or account servicing e-mails.

Brian Diggs

unread,
May 9, 2014, 6:03:40 PM5/9/14
to adam.lovelan...@public.gmane.org, ggplot2
On 5/9/2014 2:07 PM, adam.lovelan...@public.gmane.org wrote:
> Hi,
>
> Can you use stat_summary along with geom_text in order to label the
> position(s) of certain location?
>
> For example, I expected the following code to label the point, (5,5) and
> no others, but it labels all of them.
>
> Is there an easy way to do the same thing, without figuring out the
> location outside of ggplot2?
>
> tst<-data.frame(x=1:5,y=1:5,z=c("a","b","c","d","e"))
>
> ggplot(tst,aes(x,y))+
> geom_point()+
> stat_summary(fun.y="max",geom="text",aes(label=z))

You can do it inside ggplot insomuch as you don't have to assign to a
second data.frame, but do the computation inline:

ggplot(tst,aes(x,y))+
geom_point()+
geom_text(data=subset(tst, y==max(y)), aes(label=z))

Dennis Murphy

unread,
May 9, 2014, 6:07:54 PM5/9/14
to adam.l...@pnc.com, ggplot2
In addition:

ggplot(tst, aes(x, y)) +
geom_point() +
geom_text(data = tst[which.max(tst$y), ], aes(label = z),
vjust = -1)

# In case you have multiple maxima,
ggplot(tst, aes(x, y)) +
geom_point() +
geom_text(data = tst[tst$y == max(tst$y), ], aes(label = z),
vjust = 1)

Dennis
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

adam.l...@pnc.com

unread,
May 12, 2014, 9:44:41 AM5/12/14
to Dennis Murphy, Brian Diggs, ggplot2
But if there's a grouping/colouring aesthetic, it sounds like an outside computation will be needed? That's why I was hoping stat_summary would do the job, but I guess I will create a summary table outside and then pass it in.

Seems odd that ggplot2 is closed off to future enhancements. What's the upside?  What happens when someone's willing to do the work to add features like these? Or a new "stat" ? Will that still be possible somehow?
 



Adam Loveland



From: Dennis Murphy <djm...@gmail.com>
To: adam.l...@pnc.com
Cc: ggplot2 <ggp...@googlegroups.com>
Date: 05/09/2014 06:08 PM
Subject: Re: geom text with stat_summary
Sent by: ggp...@googlegroups.com


Ista Zahn

unread,
May 12, 2014, 11:54:31 AM5/12/14
to adam.l...@pnc.com, Dennis Murphy, Brian Diggs, ggplot2
On Mon, May 12, 2014 at 9:44 AM, <adam.l...@pnc.com> wrote:
But if there's a grouping/colouring aesthetic, it sounds like an outside computation will be needed? That's why I was hoping stat_summary would do the job, but I guess I will create a summary table outside and then pass it in.

Seems odd that ggplot2 is closed off to future enhancements. What's the upside?  What happens when someone's willing to do the work to add features like these? Or a new "stat" ? Will that still be possible somehow?

​Nothing is closed off, pull requests will still be considered; see https://groups.google.com/forum/#!topic/ggplot2/SSxt8B8QLfo for details.

Best,
Ista​

 
Reply all
Reply to author
Forward
0 new messages