How to resolve the error in R while plotting the decision boundary




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

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

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!!


@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!