Search code examples
rsweave

Rendering grid graphics in Sweave


I have a short routine which arranges ggplot and grid graphics using the grid.arrange function in the package gridExtras. I need to render the output from my routine (which nominally prints using grid.draw, or returns the grid object as an option) in my Sweave documentation. I'm at a loss how to do this, since the say I've been using "print" doesn't work the same way as with a pure ggplot graphic. I've tried:

g <- ggkm(survfit.object, returns=T)
print(g)

where g is the object created by grid.arrange, and has class

> class(g)
[1] "frame" "gTree" "grob"  "gDesc"

Any help would be appreciated

Abhijit


Solution

  • Printing the object as it's created rather than printing the saved object seems to work, although I couldn't explain why ...

    \documentclass{article}
    \begin{document}
    
    
    <<>>=
    library(ggplot2)
    library(gridExtra)
    
    d <- data.frame(x=1:10,y=1:10,z=runif(10))
    g1 <- qplot(x,y,data=d)
    g2 <- qplot(x,z,data=d)
    @ 
    
    <<fig=TRUE,results=hide>>=
    print(grid.arrange(g1,g2,ncol=2))
    @ 
    
    \end{document}
    

    enter image description here