如何使用 ggplot 绘制 T-SNE 聚类

How to use ggplot to plot T-SNE clustering

这是使用 IRIS 数据的 t-SNE 代码:

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


# Show the objects in the 2D tsne representation
plot(tsne_out$Y,col=iris_unique$Species)

产生这个情节的是:

我如何使用 GGPLOT 制作该图?

我认为 easiest/cleanest ggplot 方法是将您需要的所有信息存储在 data.frame 中,然后绘制它。从上面粘贴的代码来看,这应该有效:

library(ggplot2)
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))

我使用常规 plot 函数的情节是:

plot(tsne_out$Y,col=iris_unique$Species)