Editing default functions: Changing the default color of plot function in kernlab in R

331 Views Asked by At

Per the example in the kernlab documentation, plot makes a nice figure of the decision weights and boundary of an SVM model.

require(kernlab)
x<- rbind(matrix(rnorm(n=120,mean=-1,sd=2),,2),matrix(rnorm(120,mean=3,sd=2),,2))
y <- matrix(c(rep(1,60),rep(-1,60)))
svp <- ksvm(x,y,type="C-svc",kernel="vanilladot")
plot(svp,data=x)

However, I'd like to change the default colors of the background gradient. Any suggestions how to do this? I've looked into edit(plot), but am not comfortable enough with generic functions to know what to change. Thanks!

1

There are 1 best solutions below

0
On

If you check with showMethods("plot") and then getMethod("plot", c("ksvm", "missing")), you'll see that the plotting function creates its own colors:

[...]
mycols <- c(hcl(0, 100 * (nl:0/nl)^1.3, 90 - 40 * 
   (nl:0/nl)^1.3), rev(hcl(260, 100 * (nl:0/nl)^1.3, 
   90 - 40 * (nl:0/nl)^1.3)))
[...]

and uses them for plotting:

[...]
filled.contour(xr, yr, matrix(as.numeric(preds), 
    nrow = length(xr), byrow = TRUE), col = mycols,
[...]

which means that you cannot change the colors easily, they are hardcoded into the ksvm plot().

What you can do is

  1. Making a copy of the plot() function, and rewrite it, such that you can overwrite the default colors.
  2. Write an email to the maintainer of the package, and tell him to add this feature. Possibly send him a patch, if you already rewrote the plotting function.
  3. "Edit the function in place. See What ways are there to edit a function in R?.