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