ggplot2 with class "varImp.train"

I have been reproducing the script from the following site:
Create predictive models in R with Caret
but when trying to replay the command:

ggplot(varImp(model.cv))
Error in .Call.graphics(C_palette2, .Call(C_palette2, NULL)) : 
  invalid graphics state

The characteristics and the class returned by the function varImp() are:

str(varImp(model.cv))
List of 3
 $ importance:'data.frame':	10 obs. of  1 variable:
  ..$ Overall: num [1:10] 77 100 82.7 38.1 93.2 ...
 $ model     : chr "loess r-squared"
 $ calledFrom: chr "varImp"
 - attr(*, "class")= chr "varImp.train"
> class(varImp(model.cv))
[1] "varImp.train"

The solution I found was the following:

ss<-varImp(model.cv)
data<-data.table(name=row.names(ss$importance),value=ss$importance$Overall)
data[,ggplot(.SD, aes(x=reorder(name, value), y=value)) + 
  geom_bar(stat = "identity")+
  coord_flip(),]

Is there another way to deal with this problem?
Why in my case the command ggplot (varImp (model.cv))
It was not the same as on the site?

Strange, it does work directly on my computer. The error message suggests to me it could be a problem with the previous graph you plotted, have you tried dev.off() until you get back to the null device, then plotting again?

With R 4.0.0, caret_6.0-86, ggplot2_3.3.2, and lattice_0.20-41.

303/5000

I closed RStudio and opened it again by running the script again and everything
solved.
What command should I put at the beginning of the script to initialize all the environment, Plot and console?
I know, separately
rm (list = ls ()) Empty Environment,
...? null device Plot
...? Ctr + L; Clear Console from keyboard

I don't feel this is necessary, personally I just use ctrl+shift+F10 to restart the R session regularly and rerun the script. Then I guess dev.off() would close any open graphical device, and you could imagine something like while(dev.cur()) dev.off(), but in practice buggy graphical devices are rare, and ctrl+shift+F10 will close them anyway.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.