Use of grid.layout and recordPlot()

1,517 views
Skip to first unread message

Kim

unread,
May 17, 2012, 3:13:32 PM5/17/12
to ggp...@googlegroups.com
Dear group,
 
I am trying to use grid.layout to put together some pre-recorded plots (made using the recordPlot()) function.  Here are some questions:
 
1. I am using the recordPlot() function because I plotted a graph using ggplot2, and manually removed part of the plot using grid.remove() (like in Kohske's dirty hack - http://stackoverflow.com/questions/7410525/align-ggplot2-axes-using-grid).  I couldn't think of another way to save this new plot into an object except by using recordPlot().  Is there another way that is recommended?
 
2. Once I saved my recorded plots into objects, I'd like to plot each plot on the same grid using grid.layout().  Using the code below results test.png only displaying one of the plots, not all three of the plots as specified.  Any ideas why this is the case and how to fix this?
 
Thank you in advance for your help!
Kim
 
set.seed(1410)
dsmall=diamonds[sample(nrow(diamonds),100),]
qplot(carat,price,data=dsmall,geom=c("point"))
a=recordPlot()
b=recordPlot()
c=recordPlot()

png("test.png",width=3300,height=2550)
grid.newpage()
pushViewport(viewport(layout=grid.layout(4,2)))
vplayout=function(x,y)
 viewport(layout.pos.row=x,layout.pos.col=y)
print(a,vp=vplayout(1:2,1))
print(b,vp=vplayout(1:2,2))
print(c,vp=vplayout(3:4,1))
dev.off()

Dennis Murphy

unread,
May 17, 2012, 4:43:34 PM5/17/12
to Kim, ggp...@googlegroups.com
Hi:

recordPlot() isn't going to work with grid graphics, I don't think. I
can confirm that your way doesn't produce three graphs on a page using
grid. I also tried the grid.arrange() function in the gridExtra
package, and that turned out to be illuminating. Note that
recordPlot() comes from the grDevices package in base R.

set.seed(1410)
dsmall=diamonds[sample(nrow(diamonds),100),]
qplot(carat,price,data=dsmall,geom=c("point"))
a=recordPlot()
b=recordPlot()
c=recordPlot()

library('grid')
grid.newpage()
pushViewport(viewport(layout=grid.layout(4,2)))
vplayout=function(x,y)
viewport(layout.pos.row=x,layout.pos.col=y)
print(a,vp=vplayout(1:2,1))
print(b,vp=vplayout(1:2,2))
print(c,vp=vplayout(3:4,1))

As I'm sure you found, this renders one graph at a time, each of which
takes up a complete graphics page. You could say that the graphs are
'grid-unaware'. Trying this with gridExtra,

library('gridExtra')
grid.arrange(a, b, c, ncol = 2)

Error in arrangeGrob(..., as.table = as.table, clip = clip, main = main, :
input must be grobs!
...which tells you that the output of recordPlot() does not yield
grobs that the grid package can recognize.

OTOH, if you do

a <- qplot(carat,price,data=dsmall,geom=c("point"))
b <- qplot(carat,price,data=dsmall,geom=c("point"))
c <- qplot(carat,price,data=dsmall,geom=c("point"))

and use the code above, both will print as expected because a ggplot
graphic is a grob (graphical object). It seems that what you need is a
way to edit/modify your plots in order to return grobs with which the
grid package can work. Perhaps the grid experts on this list can
provide more specific details.

HTH,
Dennis
> --
> 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

baptiste auguie

unread,
May 17, 2012, 5:04:53 PM5/17/12
to Dennis Murphy, Kim, ggp...@googlegroups.com
Hi,

recordPlot records the displaylist, which I think is valid for both
base and grid graphics. However, its print method has no extra
argument, let alone a viewport,

getS3method("print","recordedplot")

Insted, I would suggest recording the modified ggplots as grobs, e.g.
with grid::grid.grabExpr or grid.grab. These can be redrawn in
specific viewports with either grid.arrange or grid.draw.

HTH,

baptiste
Reply all
Reply to author
Forward
0 new messages