plyr and ggplot and environments

46 views
Skip to first unread message

Andreas Leha

unread,
Sep 25, 2013, 5:17:33 AM9/25/13
to ggp...@googlegroups.com
Hi all,

I have fallen repeatedly into this: How can I produce a plot inside a,
say, =llply= which uses a local variable that is defined inside the
llply?

Here is an example:
--8<---------------cut here---------------start------------->8---
library("plyr")
library("ggplot2")

testlist <- list("aaa", "bbb")

testplots <-
llply(testlist, function(label)
{
p <- ggplot(data.frame(x=1:10, y=1:10)) + geom_point(aes(x=x, y=y))
p <- p + geom_text(aes(x=0.5, y=0.5, label=label))
p
})

testplots[[1]]
## Error in eval(expr, envir, enclos) : object 'label' not found


## NB: the version outside an llply works:
label <- "aaa"
p <- ggplot(data.frame(x=1:10, y=1:10)) + geom_point(aes(x=x, y=y))
p <- p + geom_text(aes(x=0.5, y=0.5, label=label))
p
--8<---------------cut here---------------end--------------->8---


What is the proposed solution here?

Regards,
Andreas

Andreas Leha

unread,
Sep 25, 2013, 6:40:35 AM9/25/13
to ggp...@googlegroups.com
Andreas Leha
<andrea...@med.uni-goettingen.de> writes:

> Hi all,
>
> I have fallen repeatedly into this: How can I produce a plot inside a,
> say, =llply= which uses a local variable that is defined inside the
> llply?
>
> Here is an example:
> library("plyr")
> library("ggplot2")
>
> testlist <- list("aaa", "bbb")
>
> testplots <-
> llply(testlist, function(label)
> {
> p <- ggplot(data.frame(x=1:10, y=1:10)) + geom_point(aes(x=x, y=y))
> p <- p + geom_text(aes(x=0.5, y=0.5, label=label))
> p
> })
>
> testplots[[1]]
> ## Error in eval(expr, envir, enclos) : object 'label' not found
>
>
> ## NB: the version outside an llply works:
> label <- "aaa"
> p <- ggplot(data.frame(x=1:10, y=1:10)) + geom_point(aes(x=x, y=y))
> p <- p + geom_text(aes(x=0.5, y=0.5, label=label))
> p
>
>
> What is the proposed solution here?
>
> Regards,
> Andreas


Sorry to answer my own post.

The solution in that case is simple, of course. I just have to pass the
used data as data=xxx.

I'll come back, if I find one of my previous examples with no obvious
solution.

Until then, sorry for the noise.

- Andreas

David Guy

unread,
Sep 25, 2013, 8:43:22 AM9/25/13
to Andreas Leha, ggplot2
Andrea,

I think the problem is that the label=label must be taken outside the aes of geom_text. Then it works fine.

David



--
--
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/groups/opt_out.



--
David Guy, PhD
R Statistical Consulting Inc.
Flemington, NJ USA
 
Office: 908-237-5107
Cell:    917-941-5890
Fax:    908-284-0356
Skype: david.guy1213


Reply all
Reply to author
Forward
0 new messages