Parameterizing column in aes

24 views
Skip to first unread message

Stavros Macrakis

unread,
Sep 26, 2011, 2:47:19 PM9/26/11
to ggplot2
I can parameterize what variable I want to plot in ggplot by constructing my own 'aes' object, like this:

data <- data.frame(a=1:10,b=11:20)
col <- quote(b)   # parameter to below
ggplot(data) + geom_point( structure(list(x=quote(a), y= col ), class= "uneval"))

or
aese <- function(...) structure(eval(substitute(list(...))), class="uneval")
ggplot(data) + geom_point( aese(x=quote(a), y= col ))

Is there a cleaner or more standard way to do this?

            -s

Brian Diggs

unread,
Sep 26, 2011, 3:22:33 PM9/26/11
to ggplot2

There is aes_string

col <- "b"

ggplot(data) + geom_point(aes_string(x="a", y=col))

> -s

--
Brian S. Diggs, PhD
Senior Research Associate, Department of Surgery
Oregon Health & Science University

Adam_L...@keybank.com

unread,
Sep 26, 2011, 3:45:57 PM9/26/11
to ggp...@googlegroups.com
Was having trouble labelling the mean that's calculated using stat_summary. Taking initial example (now with group added; thanks Kohske),

tst.data <- data.frame (x = c(rep("Blue",500), rep("Red",500)), y=c(rnorm(500,3,6), rnorm(500,8,4) ) )
ggplot ( data = tst.data,aes(x,y)) + geom_point(alpha = .3) +
stat_summary(aes(group=1),fun.y = "mean",geom = "line")


Above works.

tst.data <- data.frame (x = c(rep("Blue",500), rep("Red",500)), y=c(rnorm(500,3,6), rnorm(500,8,4) ) )
ggplot ( data = tst.data,aes(x,y)) + geom_point(alpha = .3) +

stat_summary(aes(group=1),fun.y = "mean",geom = "line") +
stat_summary(aes(group=1),fun.y = "mean",geom = "text")


This doesn't. It asks for the label, but I'm not sure how to add it (adding it as I would a usual aesthetic doesn't work). And of course, once I add it, I'd like to be able to format it somehow (e.g. dollar).

Thanks again.



Adam Loveland

Adam Loveland
216-689-5409
1Key

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.

Brian Diggs

unread,
Sep 26, 2011, 5:13:29 PM9/26/11
to ggplot2
On 9/26/2011 12:45 PM,
Adam_Loveland-QlCk...@public.gmane.org wrote:
> Was having trouble labelling the mean that's calculated using
> stat_summary. Taking initial example (now with group added; thanks
> Kohske),
>
> tst.data<- data.frame (x = c(rep("Blue",500), rep("Red",500)),
> y=c(rnorm(500,3,6), rnorm(500,8,4) ) )
> ggplot ( data = tst.data,aes(x,y)) + geom_point(alpha = .3) +
> stat_summary(aes(group=1),fun.y = "mean",geom = "line")
>
> Above works.
>
> tst.data<- data.frame (x = c(rep("Blue",500), rep("Red",500)),
> y=c(rnorm(500,3,6), rnorm(500,8,4) ) )
> ggplot ( data = tst.data,aes(x,y)) + geom_point(alpha = .3) +
> stat_summary(aes(group=1),fun.y = "mean",geom = "line") +
> stat_summary(aes(group=1),fun.y = "mean",geom = "text")
>
> This doesn't. It asks for the label, but I'm not sure how to add it
> (adding it as I would a usual aesthetic doesn't work). And of course, once
> I add it, I'd like to be able to format it somehow (e.g. dollar).

You have to specify it as the returned value from the summary.

ggplot ( data = tst.data,aes(x,y)) + geom_point(alpha = .3) +
stat_summary(aes(group=1),fun.y = "mean",geom = "line") +

stat_summary(aes(group=1, label=ggplot2::dollar(..y..)), fun.y = "mean",
geom = "text")

I don't know why you have to have ggplot2::dollar rather than just
dollar, though.

> Thanks again.
>
>
>
> Adam Loveland
>
> Adam Loveland
> 216-689-5409
> 1Key
>
> 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:DNERequests...@public.gmane.org with 'No Promotional E-mails' in the
> SUBJECT line.
>


Adam_L...@keybank.com

unread,
Sep 26, 2011, 5:38:15 PM9/26/11
to Brian Diggs, ggplot2
Brian,

Thanks a lot. This solution probably would not have occurred to me (wasn't thinking about stat_summary in the right way). And the formatting part I definitely could not have figured out.  

Adam Loveland


with 'No Promotional E-mails' in the
--
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

Reply all
Reply to author
Forward
0 new messages