<p>Although a bit late, here is the solution for this:</p>

<p>1) Create color/column levels:</p>

<pre><code>levels = unlist(lapply(colnames(trn[,1:16]), function(x)
    {

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


}))
</code></pre>

<p>2) </p>

<pre><code>plot(tsne$Y, col=factor(levels))
</code></pre>

<p>This should color columns H1 and H2 differently</p>

<p>Edit:</p>

<p>This is a sample complete code:</p>

<pre><code>library(Rtsne)
file1&lt;- read.csv('filename.csv',header = T, row.names=1)
trn &lt;- data.matrix(file1)
dim(trn)

tsne &lt;- 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)
</code></pre>

CC BY-SA 3.0
2018-05-01T06:04:56.143Z
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
CC BY-SA 4.0
2018-04-30T05:55:29.227Z
2018-05-02T15:19:26.797Z
2018-05-02T15:19:26.797Z
ayman
<p>It seems that <code>tsne$Y</code> 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.</p>

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

CC BY-SA 3.0
2017-12-15T04:52:34.917Z
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
CC BY-SA 3.0
2017-12-15T07:55:33.370Z
Do you think that adding the labels of 3000 genes will help to visualize the plot? How will this help you?
llrs
CC BY-SA 3.0
2017-12-15T08:07:13.190Z
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
CC BY-SA 3.0
2017-12-15T08:18:38.343Z
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
CC BY-SA 3.0
2017-12-15T10:51:17.237Z
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
CC BY-SA 3.0
2017-12-15T11:23:42.430Z
So, could you **edit** your post including an image similar to the desired output? I don't grasp what do mean.
llrs
CC BY-SA 3.0
2017-12-15T11:30:25.613Z
okay will do that
kcm
CC BY-SA 3.0
2017-12-14T21:33:27.973Z
2017-12-14T21:36:11.533Z
2017-12-14T21:36:11.533Z
llrs
<p>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).</p>

<p>I have a data set of total 16 cell type and about 3000 genes.</p>

<p>This is how my data is first column is gene rest is sample:</p>

<pre><code>gene    H1  H2  H3  H4  M2  M3  M4  C1  C2  C3  C4  G2  G3  G4  Gran1   Gran2
</code></pre>

<p>The code used:</p>

<pre class="lang-r prettyprint-override"><code>library(Rtsne)
file1&lt;- read.csv('UNIQUE_WGCNA_FILE.csv',header = T,row.names = 1)
trn &lt;- data.matrix(file1)
dim(trn)

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

cols &lt;- 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")
</code></pre>

<p>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.</p>

<p><a href="https://i.sstatic.net/uoILK.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/uoILK.png" alt="t_sne_without_label"></a></p>

CC BY-SA 3.0
2017-12-13T18:20:18.273Z
What kind of labels are you looking for?
burger
CC BY-SA 3.0
2017-12-14T04:45:30.627Z
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
CC BY-SA 3.0
2017-12-14T08:05:56.143Z
@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
CC BY-SA 3.0
2017-12-14T09:35:46.950Z
@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
CC BY-SA 3.0
2017-12-14T10:33:50.210Z
@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
<r><rna-seq>
t-sne code and labelling issue for rna seq data
# t-sne code and labelling issue for rna seq data

(Asked by: username_0 on 2017-12-13)

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)
file1<- read.csv('UNIQUE_WGCNA_FILE.csv',header = T,row.names = 1)
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.

**Tags:** r, rna-seq
> **Comments:**
> **username_1**: What kind of labels are you looking for?  
> **username_0**: 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  
> **username_2**: @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).  
> **username_0**: @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  
> **username_0**: @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  

---

## 2 answers

### Answer by username_3 on 2018-04-30. Score: 3 | Accepted answer

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)
file1<- read.csv('filename.csv',header = T, row.names=1)
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)

> **Comments:**
> **username_0**: 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  

---

### Answer by username_2 on 2017-12-14. Score: 1

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.

> **Comments:**
> **username_0**: 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 ..  
> **username_2**: Do you think that adding the labels of 3000 genes will help to visualize the plot? How will this help you?  
> **username_0**: 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  
> **username_2**: 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  
> **username_0**: 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.  
> **username_2**: So, could you **edit** your post including an image similar to the desired output? I don't grasp what do mean.  
> **username_0**: okay will do that  

---

