Search code examples
rlibsvm

Is there a workaround to change the plot.svm hardcoded title?


Consider the following code. I am trying to pass the parameter main to change the plot title to "FooBar", but it seems to be hardcoded to "SVM Classification Plot". I also tried to use the function call title but that has the effect of overlaying the two titles, which is even more undesirable. Is there any workaround for this?

library(e1071)



pdf("Play.pdf")
# Generate data
set.seed(1)
x=matrix(rnorm(200*2),ncol=2)
x[1:100,]=x[1:100,]+2
x[101:150,]=x[101:150,]-2
y=c(rep(1,150),rep(2,50))
dat=data.frame(x=x,y=as.factor(y))

train=sample(200,100)

svmfit=svm(y~.,data=dat[train,],kernel="radial", cost=1, gamma=1)
plot(svmfit,dat[train,], main="FooBar")

dev.off()

Solution

  • The main title is hardcoded, so you need to modify the code of the function.

    To avoid messing with the package I would suggest creating a copy of the function in the global environment and use that.

    For instance:

    myplotSVM <- e1071:::plot.svm
    environment(myplotSVM)  <- .GlobalEnv
    fix(myplotSVM)
    

    Then change the function definition as:

    function (x, data, formula = NULL, fill = TRUE, grid = 50, slice = list(), 
      symbolPalette = palette(), svSymbol = "x", dataSymbol = "o", 
      main="SVN classification plot", ...) 
    

    and then, on line 56

    plot.title = title(main = main, # <----- change this part!!! 
                    xlab = names(lis)[2], ylab = names(lis)[1]), 
                    ...)
    

    So that the title will be SVN classification plot, or whatever you provide as main parameter

    You can now use it as

    myplotSVM(svmfit,dat[train,], main="FooBar")