Funtion created ggplot to ggplot object
<p>How to create a <code>ggplot</code> object from a function generated plot. I am using a function for creating dual y-axis plot in <code>ggplot</code> and it works, but I cannot convert it to a <code>ggplot</code> object with class <code>gg</code> and <code>ggplot</code> like other <code>ggplot</code>objects.</p>

<p>Like if we have two plots like these: </p>

<pre><code>p1 &lt;- qplot(wt, mpg, data=mtcars)
p2 &lt;- ggplot(diamonds, aes(price, colour = cut)) + geom_density()
</code></pre>

<p>Now I combined these two using these codes and produced the third plot:</p>

<pre><code>p3 &lt;- grid.arrange(p1,p2,ncol=2)
</code></pre>

<p>It produced the plot, but I cannot recall the <code>p3</code> plot later, means it didn't render as <code>ggplot</code> as it did for <code>p1</code> or <code>p2</code>.</p>

<p>I don't want to export as pdf or image file right now. My idea is to use the <code>p3</code> plot later and combine with other <code>ggplots</code>.</p>