Question: How to assign colors by given values for t-SNE plot
0
marshelma0 wrote:

I have a matrix, each data point in this matrix have Binomial variable `X1` to `Xm`, and a continuous variable `Y`.

I used `Rtsne()` in R to separate all data points by variable `X1` to `Xm`. After that, instead of clustering them by k-mean or `hcluster`, what I want is to add color on all dots based on their `Y` values to see how the dots with different `Y` value are separated.

Could anyone tell me how to add color for this purpose?

The following code are used to generate the s-SNE plot and draw the plot:

``````tsne_model_1 = Rtsne(as.matrix(term_tsne), check_duplicates=FALSE, pca=TRUE, perplexity=10, theta=0.5, dims=2)
d_tsne_1 = as.data.frame(tsne_model_1\$Y)
ggplot(d_tsne_1, aes(x=V1, y=V2)) +
geom_point(size=0.25) +
guides(colour=guide_legend(override.aes=list(size=6))) +
xlab("") + ylab("") +
ggtitle("t-SNE of 533 terms") +
theme_light(base_size=20) +
theme(axis.text.x=element_blank(),
axis.text.y=element_blank())
``````

term_tsne is the data matrix I used for t-SNE, colnames(term_tsne) return X1, X2....Xm, Y

R • 4.7k views
modified 2.4 years ago by Jean-Karim Heriche23k • written 2.4 years ago by marshelma0

If you were to provide the exact commands you use, it would be easier for people to know where you are in terms of the solution and how best they can steer you in the right direction.

Thank you! I added the code to the question.

Please use the code formatting button to make code more legible.

0
Jean-Karim Heriche23k wrote:

This is a ggplot2 question. Plenty of examples can be found online. Maybe something along the lines of

``````geom_point(aes(x=X, y=Y, color=...)) + scale_colour_*(...)
``````

Since Y is continuous, you'll need to either map values to a colour gradient or map ranges to discrete colours.

Thank you for the suggestion, I will check if I can work it out.

Sorry, I have a feature plot in seurat, how I can change the color of some cells based on my desire?