<p>If you would like to color discrete intervals on a gradient as opposed to having a continuous gradient (like your second plot), use this approach. </p>

<p>It is similar to the approach in the answer I posted with the continuous scale, but we simply break up the continuous scale in to intervals and color them by these intervals.</p>

<pre><code>#generate values for testing purposes, one value for each cell, add to object
value &lt;- sample(seq(from=8, to=46, by=1), size = length(rownames(exp@meta.data)), replace=TRUE)
exp &lt;- AddMetaData(object=exp, metadata=value, col.name="value")

#encode the continuous values as factors, determined by the interval they fall in to
value_breaks &lt;- cut(exp@meta.data$value, breaks = c(8,18,28,38,46), include.lowest=TRUE, right=FALSE)

#name the breaks by cell so they can be added by AddMetaData(), add them
names(value_breaks) &lt;- rownames(exp@meta.data)
exp &lt;- AddMetaData(object=exp, metadata=value_breaks, col.name="value_breaks")

exp &lt;-  SetIdent(exp, ident.use=exp@meta.data$value_breaks)

#create a gradient between specified intervals
#length.out is the number of different colors, number of factor levels
cols &lt;- scales::seq_gradient_pal(low="blue", high="yellow", space="Lab")(seq(from=0, to=1,length.out=4))

TSNEPlot(exp, colors.use=cols)
</code></pre>

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

<p>The label in the legend are classic mathematical notation for intervals. You can add your own labels if you wish in the <code>cut()</code> function.</p>

CC BY-SA 4.0
2018-11-01T17:46:31.660Z
Thanks a lot, you solved my long lasting question, I produced this final picture https://image.ibb.co/dsZzsf/Rplot22.png  here I am plotting 300 cell types markers hopefully to gene 2 clusters yellow and another one blue, but as you are seeing the threshold I have selected for converting mat (expression values) of these genes to a binary matrix has not been good because my tsne does not look brilliant. If you where me, what threshold you would selected to converting your expression values to binary? I have used the average of expression of these genes in clusters as threshold. for example 2
Zizogolu
CC BY-SA 4.0
2018-11-04T16:05:21.907Z
That deserves a post of its own! That exact question was asked on here a few weeks back, but I think it was removed. There were at least four different "answers." It is a difficult questions with all answers (that I've seen) with their own limitations.
Kohl Kinning
CC BY-SA 4.0
2018-11-04T16:35:12.490Z
Thank you, in CellRouter R package, developer uses a centring on data before plotting but I am not able to understand what he is doing. But his centring is very good https://github.com/edroaldo/cellrouter/blob/master/CellRouter_Class.R  in plotDRExpression  function
Zizogolu
CC BY-SA 4.0
2018-11-01T15:50:00.467Z
2018-11-01T15:50:00.467Z
Kohl Kinning
<p><strong>TSNEPlot()</strong></p>

<p><code>TSNEPlot()</code> will always treat your variables as discrete. My approach is to manually generate a gradient with unique colors for each factor level and pass it to the <code>cols.use</code> argument in <code>TSNEPlot()</code>. </p>

<pre><code>#generate values for testing purposes, one value for each cell
value &lt;- sample(seq(from=8, to=48, by=1), size = length(rownames(unfiltered_cca@meta.data)), replace=TRUE)
names(value) &lt;- rownames(exp@meta.data)
exp &lt;- AddMetaData(object=exp, metadata=value, col.name="value")
exp &lt;-  SetIdent(exp, ident.use=exp@meta.data$value)
#create a gradient between specified colors, multiply by sequence to get appropriate length
#length.out is the number of different colors, number of factor levels
cols &lt;-  scales::seq_gradient_pal(low="beige", high="red", space="Lab")(seq(from=0, to=1,length.out=48))


TSNEPlot(exp, colors.use=cols)
</code></pre>

<p><a href="https://i.sstatic.net/PKps4.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/PKps4.png" alt="enter image description here"></a></p>

<p>I would present this with no legend:</p>

<pre><code>TSNEPlot(exp, colors.use=cols, no.legend = TRUE)
</code></pre>

<p><a href="https://i.sstatic.net/Ix0cj.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Ix0cj.png" alt="enter image description here"></a></p>

<p><strong>FeaturePlot()</strong></p>

<p>You can also simply use <code>FeaturePlot()</code> instead of <code>TSNEPlot()</code> to visualize the gradient. Using the same data as above:</p>

<pre><code>FeaturePlot(object = exp, features.plot = "value", reduction.use = "tsne", no.legend = FALSE, cols.use = c("beige", "red"))
</code></pre>

<p><a href="https://i.sstatic.net/hZCd4.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/hZCd4.png" alt="enter image description here"></a></p>

<p>You ask for a continuous scale, but this is not what is shown in your second plot.  You have a different color for each discrete range of values. I assume here that you want a continuous gradient. To achieve something like the second plot you provided requires a different approach.</p>

CC BY-SA 4.0
2018-11-01T09:37:24.490Z
Sorry, your solution returned colourless featureplot
Zizogolu
CC BY-SA 4.0
2018-11-01T14:13:10.243Z
TSNEPlot() returns a colourless plot. I have used cc@meta.data$value as my value and in col I used cols <- scales::seq_gradient_pal(low="beige", high="red", space="Lab")(seq(from=10, to=48, length.out=34)), however returned a colourless lot
Zizogolu
CC BY-SA 4.0
2018-11-01T14:14:55.770Z
`seq(from=0, to=1, length.out=34)` is essential. You need a sequence of decimals for the color gradient. The `length.out` is correct.
Kohl Kinning
CC BY-SA 4.0
2018-11-01T14:15:35.850Z
Actually like my second plot I would like to have a colour range for example 8-18, 18-28, 28-38, 38-48 in gradient of colours but I don't know how to do that
Zizogolu
CC BY-SA 4.0
2018-11-01T14:18:20.243Z
Thanks a lot, now I have a colourful tsne plot, sorry do you know how to convert this wide range of colours to 4 ranges 8-18, 18-28, 28-38, 38-48 as a yellow to blue gradient like my second plot?
Zizogolu
CC BY-SA 4.0
2018-11-29T11:43:43.283Z
Sorry how I could convert cols <-  scales::seq_gradient_pal(low="beige", high="red", space="Lab")(seq(from=0, to=1,length.out=48))
  to theme_bw() +
              #scale_colour_gradientn(name = "Expression", colours=rev(rainbow(4))) +
              scale_colour_gradientn("Relative expression", colours=c("midnightblue","dodgerblue3","white", "darkorange2", "yellow"))
Zizogolu
CC BY-SA 4.0
2018-10-30T19:56:24.700Z
2018-10-30T20:12:08.690Z
2018-10-30T20:12:08.690Z
Kohl Kinning
<p>I returned a FeaturePlot from Seurat to ggplot. My plot has a weird range of colours as below</p>

<p><a href="https://i.sstatic.net/FhN0Y.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/FhN0Y.png" alt="enter image description here"></a></p>

<p>I produced this plot by this code</p>

<pre><code>&gt; head(mat[1:4,1:4])
             s1.1        s1.2 s1.3       s1.4
DDB_G0267178    0 0.009263254    0 0.01286397
DDB_G0267180    0 0.000000000    0 0.00000000
DDB_G0267182    0 0.000000000    0 0.03810585
DDB_G0267184    0 0.000000000    0 0.00000000
&gt; 

I have converted expression matrix to a binary matrix by 2 as a threshold

mat[mat &lt; 2] &lt;- 0
mat[mat &gt; 2] &lt;- 1

&gt; head(exp[1:4,1:4])
             s1.1 s1.2 s1.3 s1.4
DDB_G0267382    0    0    0    1
DDB_G0267438    0    0    0    1
DDB_G0267466    0    0    0    0
DDB_G0267476    0    0    1    0
&gt; 
&gt; exp=colSums(exp)
&gt; exp=as.matrix(exp)
&gt; colnames(exp)="value"
&gt; exp=as.data.frame(exp)
&gt; cc &lt;- AddMetaData(object = seurat, metadata = exp)
&gt; cc &lt;- SetAllIdent(object = cc, id = "value")
&gt; TSNEPlot(object = cc, do.return= TRUE)
</code></pre>

<p>How I can convert this range to a gradient of colours for example in 8-18, 18-28, 28-38, 38-48 range in blue to yellow please? Something like below</p>

<p><a href="https://i.sstatic.net/Ph8xz.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Ph8xz.png" alt="enter image description here"></a></p>

<p>Thank you for any help</p>

<p>Then by ggplot now I scaled my colours but I don't like my clusters as so and I don't know how to retain my clusters as a featureplot by this new color gradient </p>

<pre><code>&gt; head(cc@meta.data)
     nGene    nUMI    orig.ident res.0.7 CELL STAGE GENO dataset stage.nice celltype value
s1.1  4331  373762 SeuratProject       0 s1.1   H16   WT       1        H16        0    34
s1.2  5603 1074639 SeuratProject       0 s1.2   H16   WT       1        H16        0    26
s1.3  2064   49544 SeuratProject       0 s1.3   H16   WT       1        H16        0    27
s1.4  4680  772399 SeuratProject       1 s1.4   H16   WT       1        H16        1    29
s1.5  3876  272356 SeuratProject       1 s1.5   H16   WT       1        H16        1    21
s1.6  2557  122314 SeuratProject       0 s1.6   H16   WT       1        H16        0    31

&gt; ggplot(as.data.frame(cc@meta.data), aes(x = cc@meta.data<span class="math-container">$CELL, y = cc@meta.data$</span>res.0.7, colour =cc@meta.data$value)) + 
+     geom_point(size = 5) +
+     scale_colour_gradient(low = "yellow", high = "blue")
</code></pre>

<p><a href="https://i.sstatic.net/GtSTm.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/GtSTm.png" alt="enter image description here"></a></p>

<p>By below code I obtained a tsne in link</p>

<pre><code>&gt; cols &lt;-  scales::seq_gradient_pal(low="beige", high="red", space="Lab")(seq(from=0, to=1,length.out=48))
&gt; 
&gt; TSNEPlot(cc, colors.use=cols)
</code></pre>

<p><a href="https://image.ibb.co/mzpZ7f/Rplot21.png" rel="nofollow noreferrer">enter link description here</a></p>

<p>Now I want to know how I could convert this range to a 8-18, 18-28, 28-38, 38-48 colour range as a gradient of blue to yellow?</p>

CC BY-SA 4.0
2018-10-29T19:41:55.470Z
Can you share the code used to generate the first plot? Essentially what you put in to the call to `FeaturePlot()`.
Kohl Kinning
CC BY-SA 4.0
2018-10-29T19:44:55.823Z
Yes, Sure; I will add to the main question
Zizogolu
CC BY-SA 4.0
2018-10-29T21:09:57.773Z
Looks like your value column is not treated as an integer but as a factor.
Peter Menzel
CC BY-SA 4.0
2018-10-29T21:44:56.633Z
Coloring aside, what are trying to accomplish with `>colnames(exp)="value"`? Unless I'm missing something you are trying to assign one string to a vector of values. This shouldn't work. You should be getting an error like `length of 'dimnames' [2] not equal to array extent`. Do you ultimately want a plot of the values in one column of the matrix named `exp`?
Kohl Kinning
CC BY-SA 4.0
2018-10-30T09:16:48.763Z
I missed some part, I added that. I have used colSums of binary matrix to be able to add that to seurat metadata; exp=colSums(exp)    Please don't negatively point me, my problem is really difficult to solve
Zizogolu
CC BY-SA 4.0
2018-10-30T10:36:18.457Z
The `exp` and the `mat` matrices do not match. Please show a reproducible, minimal example.  Also by converting the exp to a data.frame it might convert your numeric values to factors, which is why you see them with each factor into a different color.
llrs
CC BY-SA 4.0
2018-10-30T11:35:30.883Z
Actually I done that by ggplot, I will add to my question
Zizogolu
CC BY-SA 4.0
2018-10-30T14:22:44.943Z
@Llopis, the data tables ought not match. `exp` is a binarized version of `mat` as Feresh Teh states above.

Feresh Teh: I would think you would want `rowSums()`, that way all of the columns for a given row will be reduced to the sum and you'll have a value (expression?) for each cell. When you add metadata to a Seurat object, it will be in this format--a value for each cell.
Kohl Kinning
CC BY-SA 4.0
2018-10-30T14:32:54.023Z
@kohlkopf Unless I misunderstood `exp` is `exp <- mat;exp[exp>2] <- 1;exp[exp<2] <- 0` But they don't match the row names between the two `head`s shown.
llrs
CC BY-SA 4.0
2018-10-30T18:10:28.007Z
Ah, you are pointing out the row names specifically. We are definitely missing some of the steps here, a minimally reproducible example is a good idea.
Kohl Kinning
CC BY-SA 4.0
2018-10-30T18:57:53.717Z
I previously thought the the column names were genes. Please disregard, `colSums()` makes sense. The cell names are the `s1.1, s1.2` etc. Working on an answer.
Kohl Kinning
<r><scrnaseq><seurat><single-cell><ggplot2>
Changing a wide range of colours to a limited gradient
# Changing a wide range of colours to a limited gradient

(Asked by: username_0 on 2018-10-29)

I returned a FeaturePlot from Seurat to ggplot. My plot has a weird range of colours as below

I produced this plot by this code
> head(mat[1:4,1:4])
             s1.1        s1.2 s1.3       s1.4
DDB_G0267178    0 0.009263254    0 0.01286397
DDB_G0267180    0 0.000000000    0 0.00000000
DDB_G0267182    0 0.000000000    0 0.03810585
DDB_G0267184    0 0.000000000    0 0.00000000
> 

I have converted expression matrix to a binary matrix by 2 as a threshold

mat[mat < 2] <- 0
mat[mat > 2] <- 1

> head(exp[1:4,1:4])
             s1.1 s1.2 s1.3 s1.4
DDB_G0267382    0    0    0    1
DDB_G0267438    0    0    0    1
DDB_G0267466    0    0    0    0
DDB_G0267476    0    0    1    0
> 
> exp=colSums(exp)
> exp=as.matrix(exp)
> colnames(exp)="value"
> exp=as.data.frame(exp)
> cc <- AddMetaData(object = seurat, metadata = exp)
> cc <- SetAllIdent(object = cc, id = "value")
> TSNEPlot(object = cc, do.return= TRUE)

How I can convert this range to a gradient of colours for example in 8-18, 18-28, 28-38, 38-48 range in blue to yellow please? Something like below

Thank you for any help
Then by ggplot now I scaled my colours but I don't like my clusters as so and I don't know how to retain my clusters as a featureplot by this new color gradient 
> head(cc@meta.data)
     nGene    nUMI    orig.ident res.0.7 CELL STAGE GENO dataset stage.nice celltype value
s1.1  4331  373762 SeuratProject       0 s1.1   H16   WT       1        H16        0    34
s1.2  5603 1074639 SeuratProject       0 s1.2   H16   WT       1        H16        0    26
s1.3  2064   49544 SeuratProject       0 s1.3   H16   WT       1        H16        0    27
s1.4  4680  772399 SeuratProject       1 s1.4   H16   WT       1        H16        1    29
s1.5  3876  272356 SeuratProject       1 s1.5   H16   WT       1        H16        1    21
s1.6  2557  122314 SeuratProject       0 s1.6   H16   WT       1        H16        0    31

> ggplot(as.data.frame(cc@meta.data), aes(x = cc@meta.data$CELL, y = cc@meta.data$res.0.7, colour =cc@meta.data$value)) + 
+     geom_point(size = 5) +
+     scale_colour_gradient(low = "yellow", high = "blue")


By below code I obtained a tsne in link
> cols <-  scales::seq_gradient_pal(low="beige", high="red", space="Lab")(seq(from=0, to=1,length.out=48))
> 
> TSNEPlot(cc, colors.use=cols)

enter link description here
Now I want to know how I could convert this range to a 8-18, 18-28, 28-38, 38-48 colour range as a gradient of blue to yellow?

**Tags:** r, scrnaseq, seurat, single-cell, ggplot2
> **Comments:**
> **username_1**: Can you share the code used to generate the first plot? Essentially what you put in to the call to `FeaturePlot()`.  
> **username_0**: Yes, Sure; I will add to the main question  
> **username_2**: Looks like your value column is not treated as an integer but as a factor.  
> **username_1**: Coloring aside, what are trying to accomplish with `>colnames(exp)="value"`? Unless I'm missing something you are trying to assign one string to a vector of values. This shouldn't work. You should be getting an error like `length of 'dimnames' [2] not equal to array extent`. Do you ultimately want a plot of the values in one column of the matrix named `exp`?  
> **username_0**: I missed some part, I added that. I have used colSums of binary matrix to be able to add that to seurat metadata; exp=colSums(exp)    Please don't negatively point me, my problem is really difficult to solve  
> **username_3**: The `exp` and the `mat` matrices do not match. Please show a reproducible, minimal example.  Also by converting the exp to a data.frame it might convert your numeric values to factors, which is why you see them with each factor into a different color.  
> **username_0**: Actually I done that by ggplot, I will add to my question  
> **username_1**: @Llopis, the data tables ought not match. `exp` is a binarized version of `mat` as Feresh Teh states above.  Feresh Teh: I would think you would want `rowSums()`, that way all of the columns for a given row will be reduced to the sum and you'll have a value (expression?) for each cell. When you add metadata to a Seurat object, it will be in this format--a value for each cell.  
> **username_3**: @kohlkopf Unless I misunderstood `exp` is `exp <- mat;exp[exp>2] <- 1;exp[exp<2] <- 0` But they don't match the row names between the two `head`s shown.  
> **username_1**: Ah, you are pointing out the row names specifically. We are definitely missing some of the steps here, a minimally reproducible example is a good idea.  
> **username_1**: I previously thought the the column names were genes. Please disregard, `colSums()` makes sense. The cell names are the `s1.1, s1.2` etc. Working on an answer.  

---

## 2 answers

### Answer by username_1 on 2018-11-01. Score: 3 | Accepted answer

If you would like to color discrete intervals on a gradient as opposed to having a continuous gradient (like your second plot), use this approach. 
It is similar to the approach in the answer I posted with the continuous scale, but we simply break up the continuous scale in to intervals and color them by these intervals.
#generate values for testing purposes, one value for each cell, add to object
value <- sample(seq(from=8, to=46, by=1), size = length(rownames(exp@meta.data)), replace=TRUE)
exp <- AddMetaData(object=exp, metadata=value, col.name="value")

#encode the continuous values as factors, determined by the interval they fall in to
value_breaks <- cut(exp@meta.data$value, breaks = c(8,18,28,38,46), include.lowest=TRUE, right=FALSE)

#name the breaks by cell so they can be added by AddMetaData(), add them
names(value_breaks) <- rownames(exp@meta.data)
exp <- AddMetaData(object=exp, metadata=value_breaks, col.name="value_breaks")

exp <-  SetIdent(exp, ident.use=exp@meta.data$value_breaks)

#create a gradient between specified intervals
#length.out is the number of different colors, number of factor levels
cols <- scales::seq_gradient_pal(low="blue", high="yellow", space="Lab")(seq(from=0, to=1,length.out=4))

TSNEPlot(exp, colors.use=cols)


The label in the legend are classic mathematical notation for intervals. You can add your own labels if you wish in the cut() function.

> **Comments:**
> **username_0**: Thanks a lot, you solved my long lasting question, I produced this final picture https://image.ibb.co/dsZzsf/Rplot22.png  here I am plotting 300 cell types markers hopefully to gene 2 clusters yellow and another one blue, but as you are seeing the threshold I have selected for converting mat (expression values) of these genes to a binary matrix has not been good because my tsne does not look brilliant. If you where me, what threshold you would selected to converting your expression values to binary? I have used the average of expression of these genes in clusters as threshold. for example 2  
> **username_1**: That deserves a post of its own! That exact question was asked on here a few weeks back, but I think it was removed. There were at least four different "answers." It is a difficult questions with all answers (that I've seen) with their own limitations.  
> **username_0**: Thank you, in CellRouter R package, developer uses a centring on data before plotting but I am not able to understand what he is doing. But his centring is very good https://github.com/edroaldo/cellrouter/blob/master/CellRouter_Class.R  in plotDRExpression  function  

---

### Answer by username_1 on 2018-10-30. Score: 3

TSNEPlot()
TSNEPlot() will always treat your variables as discrete. My approach is to manually generate a gradient with unique colors for each factor level and pass it to the cols.use argument in TSNEPlot(). 
#generate values for testing purposes, one value for each cell
value <- sample(seq(from=8, to=48, by=1), size = length(rownames(unfiltered_cca@meta.data)), replace=TRUE)
names(value) <- rownames(exp@meta.data)
exp <- AddMetaData(object=exp, metadata=value, col.name="value")
exp <-  SetIdent(exp, ident.use=exp@meta.data$value)
#create a gradient between specified colors, multiply by sequence to get appropriate length
#length.out is the number of different colors, number of factor levels
cols <-  scales::seq_gradient_pal(low="beige", high="red", space="Lab")(seq(from=0, to=1,length.out=48))


TSNEPlot(exp, colors.use=cols)


I would present this with no legend:
TSNEPlot(exp, colors.use=cols, no.legend = TRUE)


FeaturePlot()
You can also simply use FeaturePlot() instead of TSNEPlot() to visualize the gradient. Using the same data as above:
FeaturePlot(object = exp, features.plot = "value", reduction.use = "tsne", no.legend = FALSE, cols.use = c("beige", "red"))


You ask for a continuous scale, but this is not what is shown in your second plot.  You have a different color for each discrete range of values. I assume here that you want a continuous gradient. To achieve something like the second plot you provided requires a different approach.

> **Comments:**
> **username_0**: Sorry, your solution returned colourless featureplot  
> **username_0**: TSNEPlot() returns a colourless plot. I have used cc@meta.data$value as my value and in col I used cols <- scales::seq_gradient_pal(low="beige", high="red", space="Lab")(seq(from=10, to=48, length.out=34)), however returned a colourless lot  
> **username_1**: `seq(from=0, to=1, length.out=34)` is essential. You need a sequence of decimals for the color gradient. The `length.out` is correct.  
> **username_0**: Actually like my second plot I would like to have a colour range for example 8-18, 18-28, 28-38, 38-48 in gradient of colours but I don't know how to do that  
> **username_0**: Thanks a lot, now I have a colourful tsne plot, sorry do you know how to convert this wide range of colours to 4 ranges 8-18, 18-28, 28-38, 38-48 as a yellow to blue gradient like my second plot?  
> **username_0**: Sorry how I could convert cols <-  scales::seq_gradient_pal(low="beige", high="red", space="Lab")(seq(from=0, to=1,length.out=48))   to theme_bw() +               #scale_colour_gradientn(name = "Expression", colours=rev(rainbow(4))) +               scale_colour_gradientn("Relative expression", colours=c("midnightblue","dodgerblue3","white", "darkorange2", "yellow"))  

---

