title: t-SNE plot with labels
body: <p>I have a data frame with more than 5000 lines. </p>
  
  <p>A small example with few lines:</p>
  
  <pre><code>df &lt;- structure(list(i_name = structure(c(1L, 2L, 4L, 7L, 3L, 5L, 6L, 
  9L, 10L, 8L), .Label = c("An19", "An31", "An37", "An38", "An54", 
  "An56", "An59", "An6", "An65", "An9"), class = "factor"), t_factor = c(1L, 
  1L, 0L, 2L, 1L, 0L, 1L, 0L, 2L, 0L), out = c(2.72662448798073, 
  0.857435104262549, 1.33378064244986, 1.67672325941268, 1.81053565604612, 
  2.35244174245512, 1.46331663415767, 2.79710579828126, 0.715497242875863, 
  1.80209614111343)), class = "data.frame", row.names = c(NA, -10L
  ))
  </code></pre>
  
  <p>Looks like this:</p>
  
  <pre><code>┌────┬────────┬──────────┬───────────┐
  │    │ i_name │ t_factor │    out    │
  ├────┼────────┼──────────┼───────────┤
  │  1 │ An5    │        0 │ 2.7266245 │
  │  2 │ An6    │        1 │ 0.8574351 │
  │  3 │ An5    │        1 │ 1.3337806 │
  │  4 │ An59   │        2 │ 1.6767233 │
  │  5 │ An5    │        2 │ 1.8105357 │
  │  6 │ An7    │        1 │ 2.3524417 │
  │  7 │ An6    │        2 │ 1.4633166 │
  │  8 │ An7    │        0 │ 2.7971058 │
  │  9 │ An7    │        2 │ 0.7154972 │
  │ 10 │ An6    │        0 │ 1.8020961 │
  └────┴────────┴──────────┴───────────┘
  </code></pre>
  
  <p>More lines can be produced using:</p>
  
  <pre><code>df &lt;- data.frame("i_name" = paste("An", sample(1:65, 2000, replace=T) , sep = ""), "t_factor" = sample(0:2, 2000, replace=T), "out" = runif(2000, 0.01, 2.9))
  </code></pre>
  
  <p>I would like to reduce the dimensions and check if a pattern exists using t-SNE (t-distributed stochastic neighbor embedding) or k-means and put labels like this image:</p>
  
  <p><a href="https://i.stack.imgur.com/8arht.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8arht.png" alt="t-SNE on data"></a></p>
  
  <p>So far I've done this:</p>
  
  <pre><code>tsne_model_1 = Rtsne(as.matrix(df[,2:3]), check_duplicates=FALSE, pca=TRUE, perplexity=5, theta=0.5, dims=3)
  d_tsne_1 = as.data.frame(tsne_model_1$Y)
  # k-means and hierarchical cluster models
  d_tsne_1_original=d_tsne_1
  fit_cluster_kmeans=kmeans(scale(d_tsne_1), 3)
  d_tsne_1_original$cl_kmeans = factor(fit_cluster_kmeans$cluster)
  fit_cluster_hierarchical=hclust(dist(scale(d_tsne_1)))
  ## 3 clusters
  d_tsne_1_original$cl_hierarchical = factor(cutree(fit_cluster_hierarchical, k=3))
  plot_cluster=function(data, var_cluster, palette){
    ggplot(data, aes_string(x="V1", y="V2", color=var_cluster)) +
      geom_point(size=1.25) +
      guides(colour=guide_legend(override.aes=list(size=6))) +
      xlab("") + ylab("") +
      ggtitle("") +
      theme_light(base_size=20) +
      theme(axis.text.x=element_blank(),
            axis.text.y=element_blank(),
            legend.direction = "horizontal", 
            legend.position = "bottom",
            legend.box = "horizontal") + 
      scale_colour_brewer(palette = palette) 
  }
  plot_k=plot_cluster(d_tsne_1_original, "cl_kmeans", "Accent")
  plot_h=plot_cluster(d_tsne_1_original, "cl_hierarchical", "Set1")
  library(gridExtra)
  grid.arrange(plot_k, plot_h,  ncol=2)
  </code></pre>
  
  <p>Producing this:</p>
  
  <p><a href="https://i.stack.imgur.com/B7HP3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/B7HP3.png" alt="Plot without labels"></a></p>