kNN Classification in ggplot2

How to do kNN Classification in ggplot2 with Plotly.


New to Plotly?

Plotly is a free and open-source graphing library for R. We recommend you read our Getting Started guide for the latest installation or upgrade instructions, then move on to our Plotly Fundamentals tutorials or dive straight in to some Basic Charts tutorials.

ggfortify supports stats::kmeans class. You must explicitly pass original data to autoplot function via data keyword. Because kmeans object doesn't store original data. The result will be automatically colorized by categorized cluster.

Default clustered scatterplot

library(plotly)
library(ggfortify)

set.seed(1)
p <- autoplot(kmeans(USArrests, 3), data = USArrests)

ggplotly(p)
library(plotly)
library(ggfortify)

p <- autoplot(kmeans(USArrests, 3), data = USArrests, label = TRUE, label.size = 3)

ggplotly(p)

Using cluster package

{ggfortify} supports cluster::clara, cluster::fanny, cluster::pam as well as cluster::silhouette classes. Because these instances should contains original data in its property, there is no need to pass original data explicitly.

library(plotly)
library(ggfortify)
library(cluster)

p <- autoplot(clara(iris[-5], 3))

ggplotly(p)

Plotting cluster converx

Specifying frame = TRUE in autoplot for stats::kmeans and cluster::* draws convex for each cluster.

library(plotly)
library(ggfortify)
library(cluster)

p <-  autoplot(fanny(iris[-5], 3), frame = TRUE)

ggplotly(p)

Plotting cluster probability ellipse

If you want probability ellipse, specify whatever supported in ggplot2::stat_ellipse's type keyword via frame.type option.

library(plotly)
library(ggfortify)
library(cluster)

p <- autoplot(pam(iris[-5], 3), frame = TRUE, frame.type = 'norm')

ggplotly(p)

Silhouette plot

library(plotly)
library(ggfortify)
library(cluster)

p <- autoplot(silhouette(pam(iris[-5], 3L)))

ggplotly(p)

What About Dash?

Dash for R is an open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the Plotly graphing library.

Learn about how to install Dash for R at https://dashr.plot.ly/installation.

Everywhere in this page that you see fig, you can display the same figure in a Dash for R application by passing it to the figure argument of the Graph component from the built-in dashCoreComponents package like this:

library(plotly)

fig <- plot_ly() 
# fig <- fig %>% add_trace( ... )
# fig <- fig %>% layout( ... ) 

library(dash)
library(dashCoreComponents)
library(dashHtmlComponents)

app <- Dash$new()
app$layout(
    htmlDiv(
        list(
            dccGraph(figure=fig) 
        )
     )
)

app$run_server(debug=TRUE, dev_tools_hot_reload=FALSE)