Using R plot() and plotcp() methods, we can visualize linear regression model (lm) as an equation and decision tree model (rpart) as a tree. We can develop k-nearest neighbour model using R kknn() method, but I don't know how to present this model. Please suggest me some R methods that produce nice graphs for knn model visualization.

  • $\begingroup$ Take a look here: asa.1gb.ru/kmeans/1.html $\endgroup$
    – Alex
    Jul 26, 2011 at 22:27
  • $\begingroup$ @Alex This is k-means clustering, not kNN. $\endgroup$
    – user88
    Jul 26, 2011 at 22:37
  • $\begingroup$ How many variables/features/predictors do you have? If you have two predictors, then you can just sample a grid and do predictions from your model on the grid points, then you can plot these points in different colors based on the predictions. If you have more variables, then there is not an easy way to do this. $\endgroup$
    – Gumeo
    Jul 30, 2015 at 10:57

2 Answers 2


If you want to visualize KNN classification, there's a good example here taken from the book An Introduction to Statistical Learning, which can be downloaded freely from their webpage.

They also have several neat examples for KNN regression, but I have not found the code for those.

More to the point, the package you mentioned, kknn, has built-in functionality for plot() for many of its functions, and you should browse the vignette, which contains several examples.


kNN is just a simple interpolation of feature space, so its visualization would be in fact equivalent to just drawing a train set in some less or more funky manner, and unless the problem is simple this would be rather harsh to decipher.

You may do this by counting the distances between train objects the way you did it in kknn, then use cmdscale to cast this on 2D, finally plot directly or using some smoothed scatterplot using colours to show classes or values (the smoothed regression version would require probably some hacking with hue and intensity). However, as I wrote, this would be probably a totally useless plot.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.