I'm trying to run t-sne on my coding as well as non coding genes. Even though I'm getting output I'm not confident about my code (The output it's difficult to interpret).

I have a data set of total 16 cell type and about 3000 genes.

This is how my data is first column is gene rest is sample:

gene    H1  H2  H3  H4  M2  M3  M4  C1  C2  C3  C4  G2  G3  G4  Gran1   Gran2

The code used:

file1<- read.csv('UNIQUE_WGCNA_FILE.csv',header = T,row.names = 1)
trn <- data.matrix(file1)

tsne <- Rtsne(as.matrix(trn[,1:16]), check_duplicates = FALSE, pca = FALSE, perplexity=50, theta=0.5, dims=3)

cols <- rainbow(10)
plot(tsne$Y, t='n')
text(tsne$Y, labels=as.numeric(trn[,1]), col=cols[trn[,1]])

plot3d(tsne$Y, col=cols[trn[,1]])
legend3d("topright", legend = '0':'5', pch = 16, col = rainbow(10))
rgl.postscript("Merge.pdf", "pdf")

I'm getting output but I don't know how to label so its becoming difficult. Any suggestion or help how to label would be appreciated.


  • $\begingroup$ What kind of labels are you looking for? $\endgroup$
    – burger
    Commented Dec 13, 2017 at 18:20
  • $\begingroup$ I want to label them according to my sample such as I have H1 H2 H3 H4 M2 M3 M4 C1 C2 C3 C4 G2 G3 G4 Gran1 Gran2 $\endgroup$
    – kcm
    Commented Dec 14, 2017 at 4:45
  • $\begingroup$ @krushnachChandra I've edited the question, I hope I have kept the meaning. Could you add the image of the T-SNE without the labels? Also I would like to know how have you tried to solve this (I don't want to suggest things you already tried). $\endgroup$
    – llrs
    Commented Dec 14, 2017 at 8:05
  • $\begingroup$ @Llopis first of all my code is correct? or not let me know as you know the dimension of my input , so the code is correct for my input $\endgroup$
    – kcm
    Commented Dec 14, 2017 at 9:35
  • $\begingroup$ @Llopis i have added my image and i saw the Rtsne manual i can't find ways to get the column name and label it in the plot ..so i couldn't do it..any help would be appreciated $\endgroup$
    – kcm
    Commented Dec 14, 2017 at 10:33

2 Answers 2


Although a bit late, here is the solution for this:

1) Create color/column levels:

levels = unlist(lapply(colnames(trn[,1:16]), function(x)

        if(x == "H1") return("H1")
        else if(x == "H2") return("H2")



plot(tsne$Y, col=factor(levels))

This should color columns H1 and H2 differently


This is a sample complete code:

file1<- read.csv('filename.csv',header = T, row.names=1)
trn <- data.matrix(file1)

tsne <- Rtsne(as.matrix(trn[,1:16]), check_duplicates = FALSE, pca = FALSE, perplexity=5, theta=0.5, dims=3)

collevels = unlist(lapply(colnames(trn), function(x)
  else if(grepl("H2",x))

  # etc ... for all the columns you want to color

colorColumns = factor(collevels)
plot3d(tsne$Y[,1], tsne$Y[,2], tsne$Y[,3], col=colorColumns)
  • $\begingroup$ thank you for the code i will give it a try, but do you have full tnse code if yes i would be glad if you can post in the answer i will give a try $\endgroup$
    – kcm
    Commented May 1, 2018 at 6:04

It seems that tsne$Y is the matrix and the names are just taken from there. So, you could replace the names of the columns of that matrix and then it would display the names instead of the numbers.

But, you are using the function plot3d from the rgl package, which has as arguments xlab, ylab, and zlab, so you just need to give a string to those arguments to name the axes.

  • $\begingroup$ i want to label my sample after the plot the axis isn't a problem ,because it's difficult to identify which point coming from which sample .. $\endgroup$
    – kcm
    Commented Dec 15, 2017 at 4:52
  • $\begingroup$ Do you think that adding the labels of 3000 genes will help to visualize the plot? How will this help you? $\endgroup$
    – llrs
    Commented Dec 15, 2017 at 7:55
  • $\begingroup$ nope i just want to lalel the sample name....not genes....I mean based as I have samples as these H1 H2 H3 H4 M2 M3 M4 C1 C2 C3 C4 G2 G3 G4 Gran1 Gran2 ,the H1-H4 hsc, then M2-M4 monocyte , C1-C4 CMP , G2-G4 GMP and then Gran1 and Gran2 are Granulocyte ..I want to label them in terms of my sample name ..Particular color corresponding to particular group of sample ,I hope now you can understand my issue $\endgroup$
    – kcm
    Commented Dec 15, 2017 at 8:07
  • $\begingroup$ Do you realize that you have more than 30 points in your t-SNE? No, I can't understand your issue, please edit the question and explain further what do you want $\endgroup$
    – llrs
    Commented Dec 15, 2017 at 8:18
  • $\begingroup$ okay so I will rephrase it again so just like in PCA you can label a group of sample that cluster together lets say same lineage , I would like to do the same here too. $\endgroup$
    – kcm
    Commented Dec 15, 2017 at 10:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.