Capture plot output into variable

151 views
Skip to first unread message

Leo

unread,
Nov 19, 2012, 9:03:39 PM11/19/12
to ggplot2
Hi there,

Is it possible to re-direct the plot of the ggplot object to a
variable? Something that would work like capture.output()?

Thanks!
Leo





Roman Luštrik

unread,
Nov 20, 2012, 4:11:52 AM11/20/12
to Leo, ggplot2
You can assign the ggplot call to a variable, something that is done in the help files of the function very often. Perhaps it would be clearer if you told us what you're trying to do?

Shot in the dark, but maybe you're looking for `ggsave` which saves the last plot called.

Cheers,
Roman



Leo





--
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



--
In God we trust, all others bring data.

Leo

unread,
Nov 20, 2012, 8:34:18 PM11/20/12
to ggp...@googlegroups.com, Leo
Hi Roman,

Sorry if I was not clear enough before. ggsave sinks the plot to a connection (for example a file or url or the clipboard), and the example objects in the help files are gg/ggplot objects.  These are not grid data (pixels on a matrix), not until the plot is rendered into a connection.  I want to capture the grid data, not the ggplot object. Does that make sense?

df<-data.frame(a=rnorm(20), b=rnorm(20))
p<-qplot(a,b,data=df)
class(p)

#this would not work, because the print function is directing the output to the default stdout(), 
#in this example a new window in the console, so the output (the plot data) is not captured by capture.output()
z<-capture.output(print(p))
(z)

Leo

Greg Snow

unread,
Nov 21, 2012, 6:42:22 PM11/21/12
to Leo, ggplot2
So you want to send the plot to a file?  look at the device functions like jgep, png, pdf, bitmap, etc.  You can read the plot back into R as a matrix or array using various tools like the png package or the EBImage package from bioconductor.
Gregory (Greg) L. Snow Ph.D.
538...@gmail.com

Leo

unread,
Nov 26, 2012, 5:08:58 PM11/26/12
to ggplot2
Thank you all for the replies. To Greg, yes, I can always do that, but
reading and writing to a disk takes time, especially when producing
many plots. I have tried and timed it for my needs and it just takes
too long. To ADP, plot.object is a ggplot object, not a grid.

On Nov 22, 4:21 am, ADP <nicholasehamil...@gmail.com> wrote:
> I was always under the impression you could do the following:
>
> plot.object <- ggplot(...)
> print(plot.object)
>
> meaning that a ggplot object has been assigned to the 'plot.object'
> variable.
Reply all
Reply to author
Forward
0 new messages