put look-up labels in a legend instead of on top of data

27 views
Skip to first unread message

Adam_L...@keybank.com

unread,
Jan 24, 2011, 12:10:13 PM1/24/11
to ggplot2

Sorry,

I realized my initial framing of the question may not get me the answer I need. I want to display labels in a legend, but I want to do so correspondingly to another label.

For example, below in tst there are two labels, desc and desc2. I'd like to display desc just as geom_text does by default, on the plot.

But I'd also like to display desc2 in a legend.

Basically, I have a bunch of labels that won't fit into my plot, and want to create indicators for them that do fit on the plot, but that can be "looked up" in a legend, that contains full descriptions.

The example below does the first part of what I need to do, but does not create a legend with the names, "one", "two","three", etc..

Thanks anyone in advance for your help.


tst<-data.frame(x=runif(6),y=runif(6),desc=c("1","2","3","4","5","6"),desc2=c("one","two","three","four","five","six"))
ggplot(tst,aes(x,y))+geom_point()+geom_text(aes(label=desc),size=3,vjust=-1)




Adam Loveland


----- Forwarded by Adam Loveland/GL/KB/KeyCorp on 01/24/2011 12:04 PM -----

Adam Loveland/GL/KB/KeyCorp

01/24/2011 11:58 AM

To
ggplot2 <ggp...@googlegroups.com>
cc
Subject
put labels in a legend instead of on top of data




Good noon,

I'm wondering if there's a built-in way to display labels associated with values in a legend, as opposed to on the plot (the default manner in which geom_text displays labels).

tst<-data.frame(x=runif(6),y=runif(6),desc=c("1","2","3","4","5","6"))
ggplot(tst,aes(x,y))+geom_point()+geom_text(aes(label=desc),size=3,vjust=-1)

Referring to the example above, I'd like a legend that's titled, "desc" with values 1-6, instead of the labels displayed along-side each point. I'm optimistic this is easily done in ggplot2, but couldn't figure out how to do it.

Thanks anyone in advance for your help.

Adam Loveland



Email Classification: KeyCorp Internal



Email Classification: KeyCorp Internal


This communication may contain privileged and/or confidential information. It
is intended solely for the use of the addressee. If you are not the intended
recipient, you are strictly prohibited from disclosing, copying, distributing
or using any of this information. If you received this communication in error,
please contact the sender immediately and destroy the material in its entirety,
whether electronic or hard copy. This communication may contain nonpublic personal
information about consumers subject to the restrictions of the 
Gramm-Leach-Bliley Act. You may not directly or indirectly reuse or redisclose
such information for any purpose other than to provide the services for which
you are receiving the information.

127 Public Square, Cleveland, OH 44114



If you prefer not to receive future e-mail offers for products or services from Key
send an e-mail to mailto:DNERe...@key.com with 'No Promotional E-mails' in the
SUBJECT line.

Ista Zahn

unread,
Jan 24, 2011, 6:32:56 PM1/24/11
to Adam_L...@keybank.com, ggplot2
Hi Adam,
ggplot2 isn't really set up to do this (there is no scale_numeric as
far as I know). You can kind of fake it;

ggplot(tst,aes(x,y))+geom_point()+geom_text(aes(label=desc),size=3,vjust=-1)
+ geom_blank(aes(size=paste(desc, "=", desc2)))

but it's ugly. You could probably clean it up a bit with opts() but I
don't know if you can get it to really look right.

Best,
Ista

> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2

--
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org

Adam_L...@keybank.com

unread,
Jan 24, 2011, 11:47:42 PM1/24/11
to Ista Zahn, ggplot2

And who really cares that it isn't (I don't)? For me, the fact I'm asking the question puts the fear of Cognos in my heart. In my defense, it is a legitimate statistical analysis I've under-taken. It's the legitimacy of its consumers I cannot account for.



Adam Loveland




Ista Zahn <iz...@psych.rochester.edu>
Sent by: ista...@gmail.com

01/24/2011 06:32 PM

To
Adam_L...@keybank.com
cc
ggplot2 <ggp...@googlegroups.com>
Subject
Re: put look-up labels in a legend instead of on top of data


Mike Lawrence

unread,
Nov 18, 2012, 6:03:31 PM11/18/12
to ggp...@googlegroups.com, iz...@psych.rochester.edu
This solution doesn't seem to work anymore (no legend is created). Any suggestions on a fix that will work with the current version of ggplot2?

Mike Lawrence

unread,
Nov 18, 2012, 6:05:34 PM11/18/12
to ggp...@googlegroups.com
Nevermind, figured it out:

ggplot(tst,aes(x,y))+
geom_point()+
geom_text(aes(label=desc),size=3,vjust=-1)+
geom_point(aes(size=paste(desc, "=", desc2)),colour='transparent')

will do the trick.
Reply all
Reply to author
Forward
0 new messages