CafféSospeso
CafféSospeso

Reputation: 1178

Changing ellipse line type in fviz_cluster

I am using fviz_cluster from the to plot my kmeans results, obtained using kmeans function.

Below, I'm reporting the example present in the "factoextra" package guideline.

data("iris")
iris.scaled <- scale(iris[, -5])
km.res <- kmeans(iris.scaled, 3, nstart = 25)
fviz_cluster(km.res, data = iris[, -5], repel=TRUE,
             ellipse.type = "convex")

Typing this command you will probably observe three clusters, each with a different colour. For each of those, however, I want to fix the same colour but varying line type of the ellipses. Do you know how to do it?

Upvotes: 2

Views: 2006

Answers (1)

RLave
RLave

Reputation: 8364

One solution is to use the data you get from fviz_cluster() in order to build your custom plot, by using ggplot.

Basically you just need to access the x,y coordinates of each new point, plus the info about the clusters, then you can recreate yourself the plot.

First save the data used for the plot from fviz_cluster(), then you can use chull() to find the convex hull per each cluster, then you can plot.

library(ggplot2)
library(factoextra)

# your example:
iris.scaled <- scale(iris[, -5])
km.res <- kmeans(iris.scaled, 3, nstart = 25)
p <- fviz_cluster(km.res, data = iris[, -5], repel=TRUE,
             ellipse.type = "convex") # save to access $data

# save '$data'
data <- p$data # this is all you need

# calculate the convex hull using chull(), for each cluster
hull_data <-  data %>%
  group_by(cluster) %>%
  slice(chull(x, y))

# plot: you can now customize this by using ggplot sintax
ggplot(data, aes(x, y)) + geom_point() +
  geom_polygon(data = hull_data, alpha = 0.5, aes(fill=cluster, linetype=cluster))

Of course now you can change the axis labels, add a title and add labelling per each point if you need.

enter image description here

Here an example possibly closer to your needs:

ggplot(data, aes(x, y)) + geom_point() +
  geom_polygon(data = hull_data, alpha=0.2, lwd=1, aes(color=cluster, linetype=cluster))

linetype changes the line per each cluster, you need to use lwd to make them thicker, also it's better to remove the fill argument and use color instead.

enter image description here

Upvotes: 2

Related Questions