Rtsne plot labelling
1
0
Entering edit mode
6.9 years ago
1769mkc ★ 1.2k

I m doing PCA along with RTNSE on my data set .My code

data<- read.csv('module500.csv',header = T,row.names = 1)
head(data)



set.seed(1)  # set random seed
rtsne_out <- Rtsne(as.matrix(data), pca = FALSE, verbose = TRUE)


# plot 2D t-SNE projection


plot(rtsne_out$Y, asp = 1, pch = 20, col = "red", 
     cex = 0.75, cex.axis = 1.25, cex.lab = 1.25, cex.main = 1.5, 
     xlab = "t-SNE dimension 1", ylab = "t-SNE dimension 2", 
     main = "2D t-SNE projection")

My example data file contains following data

 HSC_mean CMP_mean  GMP_mean Mono_mean Granulocyte_mean

The dim is about

> dim(data)
[1] 499   5

Im getting an image like this tnse output

Im trying how to label the cluster since this is about 499 genes and 5 sample so how do i label those I mean since I have 5 sample so 5 different colour which can label those cluster , Im not sure how do they do in the paper which i came across label each cluster...any help or suggestion would be highly appreciated

Just to make my what i'm trying to say have a look at this figure from this paper tsne figure b and the rest

R • 8.3k views
ADD COMMENT
4
Entering edit mode
6.9 years ago

You would just have to supply a colour vector to the col parameter of plot(). In your example, you just select "red", so, everything is red. The colour vector should match the order of samples in your t-SNE object that you're plotting. In your case, that's however they are ordered in rtsne_out$Y

To create a colour vector for a list of factors or just text, do something like this:

samples <- c(rep("HSC_mean",2), rep("CMP_mean",4), rep("GMP_mean",6), rep("Mono_mean",8), rep("Granulocyte_mean", 10))
samples

 [1] "HSC_mean"         "HSC_mean"         "CMP_mean"         "CMP_mean"        
 [5] "CMP_mean"         "CMP_mean"         "GMP_mean"         "GMP_mean"        
 [9] "GMP_mean"         "GMP_mean"         "GMP_mean"         "GMP_mean"        
[13] "Mono_mean"        "Mono_mean"        "Mono_mean"        "Mono_mean"       
[17] "Mono_mean"        "Mono_mean"        "Mono_mean"        "Mono_mean"       
[21] "Granulocyte_mean" "Granulocyte_mean" "Granulocyte_mean" "Granulocyte_mean"
[25] "Granulocyte_mean" "Granulocyte_mean" "Granulocyte_mean" "Granulocyte_mean"
[29] "Granulocyte_mean" "Granulocyte_mean"

require("RColorBrewer")
colours <- factor(samples, levels=c("HSC_mean", "CMP_mean", "GMP_mean", "Mono_mean", "Granulocyte_mean"))
colours <- colorRampPalette(c("royalblue", "red3", "forestgreen", "gold", "black"))(length(unique(colours)))[factor(colours)]
plot(1:length(colours), col=colours, pch=18, type="h", lwd=10)

a


You can also just assign a pre-defined colour palette from colour brewer, such as Greys, Spectral, PRGn, RdBu, or something else (here, Greys).

colours <- factor(samples, levels=c("HSC_mean", "CMP_mean", "GMP_mean", "Mono_mean", "Granulocyte_mean"))
colours <- colorRampPalette(brewer.pal(9, "PRGn"))(length(unique(colours)))[factor(colours)]
plot(1:length(colours), col=colours, pch=18, type="h", lwd=10)

b
image hosting


PS - to see all available palettes, type: require("RColorBrewer"); display.brewer.all()

ADD COMMENT
0
Entering edit mode

i got it....I can know see different color based I mean as i defined the colour of samples based on the order of my sample, another issue how do i get to label those data points , not in terms of genes, but as labelling them in terms of sample as legend , in terms of color you have defined ...

ADD REPLY
1
Entering edit mode

To label them after you have already called plot() (but it will be difficult to label all of them), you will need another vector, this time of names:

text(rtsne_out$Y, labels=MyLabels)
ADD REPLY
0
Entering edit mode

okay can i just show my color code as legend ? i think it would be difficult to put those in the plot

ADD REPLY
0
Entering edit mode

Yes, actually, when I was in the gym this morning thinking about your question, I thought that it would be easier to just show a legend. Refer to our other thread: C: heatmap data frame annotation issue

ADD REPLY

Login before adding your answer.

Traffic: 1632 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6