How to resolve the plot.new error in R while plotting the decision boundary

r
error
plotting

#1

Hello,

I am trying to plot a decision boundary after pruning a decision tree via:

i.min <- which.min(tree$cptable[,"xerror"])
i.se <- which.min(abs(tree$cptable[,"xerror"] -
                        (tree$cptable[i.min,"xerror"]
                         + tree$cptable[i.min,"xstd"])))
alpha.best <- tree$cptable[i.se,"CP"]
tree.p <- prune(tree, cp = alpha.best)

The plot is generated with:

xp <- seq(0, 1, length = 100)
yp <- seq(0, 1, length = 100)
data2dT <- expand.grid(x1 = xp, x2 = yp)
Z <- predict(tree.p, data2dT)
zp.cart <- Z[,1] - Z[,2]
contour(xp, yp, matrix(zp.cart, 100), add=T, levels=0, labcex=0.9, labels="",
        col = "green", lwd=2)

However the last line generates an error:

What is this error and how to resolve it??
Can someone please help me here!!


#2

@data_hacks-you can remove this error by changing the value of add from true to false because add is a logical it says that If TRUE, add to a current plot.

Hope this helps!
Regards,
hinduja1234