To use any other model you just need to fit it and make different predictions for yhat
glmnet:
library(glmnet)
mod <- glmnet(x, y, family = "binomial")
yhat <- predict(mod, as.matrix(newx), type = "class", s = 0.001)
plot(x, xlab="X1", ylab="X2", xlim=XLIM, ylim=YLIM)
contour(tmpx, tmpy, matrix(as.numeric(yhat),GS,GS), levels=c(1,2), add=TRUE, drawlabels=FALSE)
randomForest:
library(randomForest)
newx <- as.matrix(newx)
colnames(newx) <- NULL
mod <- randomForest(x, factor(y))
yhat <- predict(mod, as.matrix(newx))
plot(x, xlab="X1", ylab="X2", xlim=XLIM, ylim=YLIM)
contour(tmpx, tmpy, matrix(as.numeric(yhat),GS,GS), levels=c(1,2), add=TRUE, drawlabels=FALSE)