pmur002 / gridgraphics

Redraw base graphics as grid graphics
33 stars 8 forks source link

Error with recordPlot() #8

Closed robchoudhury closed 6 years ago

robchoudhury commented 6 years ago

Hi, I am trying to combine a figure with an rpart model and a ggplot figure. I use recordPlot() to capture the rpart figure. When I try to use the recordPlot object in gridGraphics, it fails.

library(gridGraphics)
library(rpart)
library(partykit)
library(broom)

rpart_model_mtcars <- rpart(mpg~.,data = mtcars, method="anova")
mtcars$predict<-predict(rpart_model_mtcars)
rpart_model_party=as.party(rpart_model_mtcars)
plot(rpart_model_party,ip_args=list() ,tp_args = list(id = FALSE))
p <- recordPlot()
p  # so far so good, plot shows

gridGraphics::grid.echo(p) # this breaks

The error it gives me is:

Error in switch(x[[2]][[1]]$name, C_abline = C_abline(x[[2]]), C_plot_new = C_plot_new(x[[2]]), : EXPR must be a length 1 vector In addition: Warning messages: 1: In FUN(X[[i]], ...) : unsupported operation on the graphics display list 2: In FUN(X[[i]], ...) : unsupported operation on the graphics display list 3: In FUN(X[[i]], ...) : unsupported operation on the graphics display list 4: In FUN(X[[i]], ...) : unsupported operation on the graphics display list 5: In FUN(X[[i]], ...) : unsupported operation on the graphics display list 6: In FUN(X[[i]], ...) : unsupported operation on the graphics display list 7: In FUN(X[[i]], ...) : unsupported operation on the graphics display list 8: In FUN(X[[i]], ...) : unsupported operation on the graphics display list

pmur002 commented 6 years ago

Hi

The problem is that you are trying to grid.echo() a plot that was drawn with 'grid'. There is no need to do that - you can just draw the "party" plot within a grid viewport, for example ...

grid.newpage()
pushViewport(viewport(width=.5, height=.5))
plot(rpart_model_party,ip_args=list() ,tp_args = list(id = FALSE), newpage=FALSE)

Does that do what you want ?

robchoudhury commented 6 years ago

Unfortunately, no. While that does output a plot, I was hoping to plot the party on top of a (pretty) version of the predicted vs. real graph using cowplot. When I asked the creator of cowplot what was going wrong, he directed me to you (https://github.com/wilkelab/cowplot/issues/80)

library(ggplot2)
library(cowplot)
library(gridGraphics)
library(rpart)
library(partykit)
library(broom)

rpart_model_mtcars <- rpart(mpg~.,data = mtcars, method="anova")
mtcars$predict<-predict(rpart_model_mtcars)
rpart_model_party=as.party(rpart_model_mtcars)
plot(rpart_model_party,ip_args=list() ,tp_args = list(id = FALSE))
p <- recordPlot() ; p

x=ggplot(mtcars, aes(predict, mpg))+
  geom_point()+
  geom_smooth(method="lm");x

summary(lm(mpg~predict, data=mtcars)) #pretty decent fit

cowplot::plot_grid(p,x)

I had excluded the cowplot bits from my original question because I thought they were extraneous, but that was obviously a mistake on my part.

pmur002 commented 6 years ago

Again, the solution is not to use recordPlot() - to capture a 'grid' plot like this you could use grid.grab(). For example ...

plot(rpart_model_party,ip_args=list() ,tp_args = list(id = FALSE))
p <- grid.grab()

... and then 'cowplot' should cope ok ...

cowplot::plot_grid(p, x)
## OR 
cowplot::plot_grid(p, x, nrow=2)

Does that work for you ?

robchoudhury commented 6 years ago

Magic! Thank you very much!