Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

scatter plot and clusters within it

I created a scatter plot using ggplot2 package for my data. Since my data has large number of points, I will explain my problem with already available small dataset. Consider this scatter plot :

ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point()

scatter plot b/w wt and mpg

I want to use k means clustering to cluster these data points, but then also show the clusters on the same scatterplot(the one shown above) and not a new dimensionality reduction plot? How can I do this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Here is an alternative using factoextra package:

library(factoextra)

df <- mtcars %>% 
  select(x = wt, y = mpg)


# Compute k-means with k = 3

set.seed(123)
res.km <- kmeans(scale(df[, -5]), 3, nstart = 25)

res.km$cluster

fviz_cluster(res.km, data = df[, -5],
             palette = c("steelblue", "gold", "limegreen"), 
             geom = "point",
             ellipse.type = "convex", 
             ggtheme = theme_bw()
)

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading