Convex hull in T-SNE plot - R

Viewed 169

Based on this question How to use ggplot to plot T-SNE clustering I wish to add convex hull to the groups but it does not work. Any reason why?

Code:

library(ggplot2)
library(Rtsne)
library(Rtsne)

iris_unique <- unique(iris) # Remove duplicates
iris_matrix <- as.matrix(iris_unique[,1:4])
set.seed(42) # Set a seed if you want reproducible results
tsne_out <- Rtsne(iris_matrix) # Run TSNE

tsne_plot <- data.frame(x = tsne_out$Y[,1], y = tsne_out$Y[,2], col = iris_unique$Species)
ggplot(tsne_plot) + geom_point(aes(x=x, y=y, color=col)) + stat_chull(aes(color = iris_unique$Species, fill = iris_unique$Species), geom = "polygon", alpha = 0.1) 
1 Answers

Use the following code

library(ggpubr)
library(ggplot2)
library(Rtsne)

iris_unique <- unique(iris) # Remove duplicates
iris_matrix <- as.matrix(iris_unique[,1:4])
set.seed(42) # Set a seed if you want reproducible results
tsne_out <- Rtsne(iris_matrix) # Run TSNE

tsne_plot <- data.frame(x = tsne_out$Y[,1], y = tsne_out$Y[,2], col = iris_unique$Species)

ggplot(tsne_plot) + geom_point(aes(x=x, y=y, color=col)) + 
  stat_chull(aes(x=x, y=y, color = col, fill = col), geom = "polygon", alpha = 0.1) 

enter image description here

Related