The first step is to understand what your code does! Indeed, you can create such a schedule without having anything to do with KNN.
For example, let's just have some sample data, where we simply โcolorizeโ the bottom quadrant of your data.
Step 1
Create a grid. Basically, how the chart works, a point is created in each coordinate, so we know which group it belongs to. in R, this is done using expand.grid to go through all possible points.
x1 <- 1:200 x2 <- 50:250 cgrid <- expand.grid(x1=x1, x2=x2) # our "prediction" colours the bottom left quadrant cgrid$prob <- 1 cgrid[cgrid$x1 < 100 & cgrid$x2 < 170, c("prob")] <- 0
If it were knn, then prob would be a prediction for this particular point.
Step 2
Now itโs relatively easy to outline. You must match the contour function, so first create a matrix with probabilities.
matrix_val <- matrix(cgrid$prob, length(x1), length(x2))
Step 3
Then you can continue as the link did:
contour(x1, x2, matrix_val, levels=0.5, labels="", xlab="", ylab="", main= "Some Picture", lwd=2, axes=FALSE) gd <- expand.grid(x=x1, y=x2) points(gd, pch=".", cex=1.2, col=ifelse(prob==1, "coral", "cornflowerblue")) box()
output:

So, back to your specific example. I'm going to use the iris because your data is not very interesting, but the same principle applies. To create a grid, you will need to select the xy axis and leave everything else fixed!
knnModel <- train(Species ~., data = iris, method = 'knn') lgrid <- expand.grid(Petal.Length=seq(1, 5, by=0.1), Petal.Width=seq(0.1, 1.8, by=0.1), Sepal.Length = 5.4, Sepal.Width=3.1)
Then just use the prediction function, as you did above.
knnPredGrid <- predict(knnModel, newdata=lgrid) knnPredGrid = as.numeric(knnPredGrid)
And then plot the graph:
pl = seq(1, 5, by=0.1) pw = seq(0.1, 1.8, by=0.1) probs <- matrix(knnPredGrid, length(pl), length(pw)) contour(pl, pw, probs, labels="", xlab="", ylab="", main= "X-nearest neighbour", axes=FALSE) gd <- expand.grid(x=pl, y=pw) points(gd, pch=".", cex=5, col=probs) box()
This should give the result as follows:

To add test / train results from your model, you can follow what I did. The only difference is you need to add the predicted points (this is not the same as the grid that was used to generate the border.
library(caret) data(iris) indxTrain <- createDataPartition(y = iris[, names(iris) == "Species"], p = 0.7, list = F) train <- iris[indxTrain,] test <- iris[-indxTrain,] knnModel <- train(Species ~., data = train, method = 'knn') pl = seq(min(test$Petal.Length), max(test$Petal.Length), by=0.1) pw = seq(min(test$Petal.Width), max(test$Petal.Width), by=0.1)
Output:

Alternatively, you can use ggplot to make graphics that can be simpler:
ggplot(data=lgrid) + stat_contour(aes(x=Petal.Length, y=Petal.Width, z=knnPredGrid), bins=2) + geom_point(aes(x=Petal.Length, y=Petal.Width, colour=as.factor(knnPredGrid))) + geom_point(data=test, aes(x=test$Petal.Length, y=test$Petal.Width, colour=as.factor(test$Pred)), size=5, alpha=0.5, shape=1)+ theme_bw()
Output:
