Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use ggplot to plot T-SNE clustering

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:

enter image description here

How can I use GGPLOT to make that figure?

like image 574
neversaint Avatar asked Jun 30 '17 02:06

neversaint


1 Answers

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

enter image description here

My plot using the regular plot function is:

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

enter image description here

like image 114
Mike H. Avatar answered Sep 29 '22 00:09

Mike H.