# t-sne code and labelling issue for rna seq data

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:

library(Rtsne)
trn <- data.matrix(file1)
dim(trn)

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

require(rgl)
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. • What kind of labels are you looking for? Dec 13, 2017 at 18:20 • 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 – kcm Dec 14, 2017 at 4:45 • @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). – llrs Dec 14, 2017 at 8:05 • @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 – kcm Dec 14, 2017 at 9:35 • @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 – kcm Dec 14, 2017 at 10:33 ## 2 Answers 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") }))  2) plot(tsne$Y, col=factor(levels))


This should color columns H1 and H2 differently

Edit:

This is a sample complete code:

library(Rtsne)
trn <- data.matrix(file1)
dim(trn)

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)
{
if(grepl("H1",x))
return("blue")
else if(grepl("H2",x))
return("red")

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

colorColumns = factor(collevels)
require(rgl)
plot3d(tsne$Y[,1], tsne$Y[,2], tsne$Y[,3], col=colorColumns)  • 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 – kcm 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.

• 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 ..
– kcm
Dec 15, 2017 at 4:52
• Do you think that adding the labels of 3000 genes will help to visualize the plot? How will this help you?
– llrs
Dec 15, 2017 at 7:55
• 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
– kcm
Dec 15, 2017 at 8:07
• 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
– llrs
Dec 15, 2017 at 8:18
• 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.
– kcm
Dec 15, 2017 at 10:51