Can hjust in geom_text be conditional?

639 views
Skip to first unread message

Adam_L...@keybank.com

unread,
Dec 16, 2011, 1:36:15 PM12/16/11
to ggp...@googlegroups.com
Hi.

Does anyone know if you can assign hjust values conditional on another value?

For example,

tst<-data.frame(a=c("a","b","d","e","f","g"),b=c(-1,1,-2,3,-2,4))

ggplot(tst,aes(a,b))+
geom_point()+
geom_segment(aes(xend=a,yend=0))+
geom_text(aes(label=b),hjust= ifelse(tst$b < 0, 1.25, - 1.25) )+
geom_hline(yintercept=0)+
coord_flip()

This does not work, but hints at what I want to do. Is there a way to do this?


Adam Loveland



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.

Kohske Takahashi

unread,
Dec 16, 2011, 2:08:25 PM12/16/11
to Adam_L...@keybank.com, ggp...@googlegroups.com
Hi

how about move the text slightly?

tst<-data.frame(a=c("a","b","d","e","f","g"),b=c(-1,1,-2,3,-2,4))

ggplot(tst,aes(a,b))+
geom_point()+
geom_segment(aes(xend=a,yend=0))+
geom_text(aes(y = b + 0.2*sign(b), label=b))+
geom_hline(yintercept=0)+
coord_flip()

--
Kohske Takahashi <takahash...@gmail.com>

Research Center for Advanced Science and Technology,
The University of  Tokyo, Japan.
http://www.fennel.rcast.u-tokyo.ac.jp/profilee_ktakahashi.html

2011/12/17 <Adam_L...@keybank.com>:

Adam_L...@keybank.com

unread,
Dec 16, 2011, 2:18:59 PM12/16/11
to Kohske Takahashi, ggp...@googlegroups.com
Very nice! Just sorry I hadn't thought of it. Thanks very much.

Adam Loveland


Reply all
Reply to author
Forward
0 new messages