How to use ggplot to plot T-SNE clustering

百般思念 提交于 2019-12-11 00:44:04

问题


Here is the t-SNE code using IRIS data:

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)

Which produces this plot:

How can I use GGPLOT to make that figure?


回答1:


I think the easiest/cleanest ggplot way would be to store all the info you need in a data.frame and then plot it. From your code pasted above, this should work:

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))

My plot using the regular plot function is:

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



来源:https://stackoverflow.com/questions/44837536/how-to-use-ggplot-to-plot-t-sne-clustering

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!