wilkelab / cowplot

cowplot: Streamlined Plot Theme and Plot Annotations for ggplot2
https://wilkelab.org/cowplot/
702 stars 84 forks source link

Weird error with 'Large recordedplot' type object #80

Closed robchoudhury closed 6 years ago

robchoudhury commented 6 years ago

I am trying to combine a partykit plot and a ggplot object so that I can show how well the predicted values aligned. I made the partykit plot into a recordedplot object, but 1) its huge, like 1.4mb+, and 2) it doesn't seem to want to get plot_grid'ded.

It spits out a weird error 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

Any help would be very much appreciated.

REPRODUCIBLE EXAMPLE:

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) #doesnt work
#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
clauswilke commented 6 years ago

This is a gridGraphics bug. Please report to the author of gridGraphics.

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

Output from last line:

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