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

R 如何使用ggplot绘制T-SNE聚类,r,ggplot2,cluster-analysis,tidyverse,R,Ggplot2,Cluster Analysis,Tidyverse,以下是使用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

以下是使用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)
库(Rtsne)

iris_unique我认为最简单/最干净的
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))

@Mike.H谢谢。但考虑到
种子(42)
,您的绘图配置与我的OP有所不同,这有点奇怪。例如,您的y轴高达~5,而我的~10.t-SNE是一个随机算法,因此每次运行它时,两个轴的值以及簇的形状都会不同。我试图用窄纵横比重现海报的绘图,因此我发现在运行每个实例之前设置.seed(…)
非常有用,以确保它是可重复的。
plot(tsne_out$Y,col=iris_unique$Species)