Reputation: 461
As a follow up to this question, suppose I do something like this:
p <- ggplot(mtcars, aes(mpg)) + geom_histogram(aes(y = ..count..)) + facet_wrap(~am)
r <- print(p)
In the second line I'm calling the print method just so that I can programmatically inspect its return value before adding additional layers to the plot object.
My question: Is there a way to suppress drawing the plot at that point?
Upvotes: 4
Views: 1916
Reputation: 173677
If you look inside ggplot2:::print.ggplot
you'll discover that what you probably want to use is either ggplot_build()
or ggplot_gtable()
, depending on what information you want to inspect.
ggplot_build
returns the data object that is invisibly returned by ggplot2's print method, so that's probably what you're after. ggplot_gtable
returns the grobs themselves, which allows for direct modification of the grid graphics objects themselves.
Upvotes: 6
Reputation: 8742
How about:
#Create a temporary plot file
png('TMP_PLOT')
#Inspect return value of plot
#When you're done
dev.off()
#Delete the plot you just generated
unlink('TMP_PLOT')
Upvotes: 2