Closed robchoudhury closed 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 ?
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.
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 ?
Magic! Thank you very much!
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.
The error it gives me is: