<p>I had this same issue. I reordered the levels and that worked for most functions, but would switch back with the FeatureHeatmap. Turns out there is a line in the function that changes it to a character. </p>

<p>I ended up making a "new" function and just changed that one line from:</p>

<pre><code>data.plot$ident &lt;- as.character(x = object@ident)
</code></pre>

<p>to:</p>

<pre><code>data.plot$ident &lt;- object@ident
</code></pre>

<p>Once that was fixed, the order that I assigned worked out just fine.</p>

CC BY-SA 4.0
2019-03-23T01:32:39.607Z
2019-03-23T01:41:40.603Z
2019-03-23T01:41:40.603Z
Amber
<p>How can I prevent a function from sorting my data groups (factors) in alphabetical order without affecting the integrity of the data? I am analysing single cell RNA sequencing data using Seurat 2.3.4. I want to be able to visualise the expression of two genes, Gene1 and Gene2 across cell clusters using the <code>FeatureHeatmap()</code> function from Seurat. I have assigned to the metadata slot of my Seurat object the factors or conditions being tested in the experiment. However on the resulting FeatureHeatmap plot, the data are sorted and grouped in alphabetical order to my disfavour. I have attempted a solution given on <a href="https://stackoverflow.com/questions/16897043/prevent-sorting-of-factor-levels">stackoverflow</a> such as the one below:</p>

<pre><code>conditions &lt;- c(rep("WTNaClctrl", 1229), 
               rep("WTBlu", 1328),
               rep("ILKOBlu", 349))


conditions &lt;- as.data.frame(conditions)

seurat_object@meta.data<span class="math-container">$conditions &lt;- factor(conditions$</span>conditions, levels=unique(conditions$conditions))
</code></pre>

<p>When I use the <code>FeatureHeatmap()</code> function, the plot <strong>still</strong> sorts the data groups in alphabetical order:</p>

<pre><code>FeatureHeatmap(seurat_object, features.plot = c("Gene1", "Gene2"), 
               pt.size = 0.25,  
               group.by = "conditions",
               max.exp = 3)
</code></pre>

<p>This plot gets produced: <a href="https://i.sstatic.net/4WCei.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/4WCei.png" alt="in alphabetical order"></a></p>

<p>I would like groupings not to appear in alphabetical order as such:
<a href="https://i.sstatic.net/6bmOv.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/6bmOv.png" alt="preferred order"></a></p>

<p>As previously suggested <a href="https://bioinformatics.stackexchange.com/questions/6838/how-to-set-the-position-of-groups-in-a-seurat-object-on-a-featureheatmap-plot/6840?noredirect=1#comment9831_6840">StackExchange</a>, I have tried to upgrade my Seurat object to Seurat V3:</p>

<pre><code>UpdateSeuratObject(Seurat_object): 
Object representation is consistent with the most current Seurat version.

devtools::install_github(repo = 'satijalab/seurat', ref = 'release/3.0') : package ‘curl’ successfully unpacked and MD5 sums checked: 

Error: (converted from warning) cannot remove prior installation of package ‘curl’.
</code></pre>

<p>Thank you in advance for any suggestions.</p>

CC BY-SA 4.0
2019-01-21T10:38:35.270Z
The error you got is from not having the rights to update curl package. Maybe it was installed by the administrator of the computer (sudo?). The plotting side of seurat seems to be done with ggplot2, which orders the categoric values based on the levels of the factors. Reorder the levels and it should go as you wish (I'd recommend `forcats`)
llrs
CC BY-SA 4.0
2019-01-21T10:45:58.040Z
@IIrs Please I am not clear about the point in the code where I should apply `forcats`.
Charles
CC BY-SA 4.0
2019-01-21T14:29:32.703Z
forcats is a package designed to make it easier to work with factors, it provides some functions to reorder the levels of the factors. I don't know seurat good enough to suggest you where you should use it.
llrs
CC BY-SA 4.0
2019-01-21T14:35:39.683Z
Possible duplicate of [How to set the position of groups in a Seurat object on a FeatureHeatmap plot](https://bioinformatics.stackexchange.com/questions/6838/how-to-set-the-position-of-groups-in-a-seurat-object-on-a-featureheatmap-plot)
TimStuart
CC BY-SA 4.0
2019-01-21T15:26:55.107Z
@TimStuart: I think in the other question, the alphabetical impact is not highlighted. So I thought of making it a new question.
Charles
CC BY-SA 4.0
2019-01-21T15:57:13.330Z
As I said in your previous question, you can't change the order using Seurat v2. The groups are coerced to a character vector which is then ordered alphabetically by ggplot2. You can either use Seurat 3 and reorder the factors, or extract the data from the object and plot it yourself
TimStuart
CC BY-SA 4.0
2019-01-21T16:33:12.377Z
@TimStuart: In the question I also highlighted my difficulty in automatically downloading the Seurat V3 package due to admin rights issues. Could I please have a link where I could download the package and load it manually?
Charles
CC BY-SA 4.0
2019-01-21T16:39:12.547Z
See the github page here: https://github.com/satijalab/seurat/tree/release/3.0 and the website here: https://satijalab.org/seurat/install.html#prerelease
TimStuart
<r><scrnaseq><seurat><ggplot2>
How do I prevent the FeatureHeatmap function from the Seurat package, from sorting my data groups in alphabetical order when plotting data?
# How do I prevent the FeatureHeatmap function from the Seurat package, from sorting my data groups in alphabetical order when plotting data?

(Asked by: username_0 on 2019-01-21)

How can I prevent a function from sorting my data groups (factors) in alphabetical order without affecting the integrity of the data? I am analysing single cell RNA sequencing data using Seurat 2.3.4. I want to be able to visualise the expression of two genes, Gene1 and Gene2 across cell clusters using the FeatureHeatmap() function from Seurat. I have assigned to the metadata slot of my Seurat object the factors or conditions being tested in the experiment. However on the resulting FeatureHeatmap plot, the data are sorted and grouped in alphabetical order to my disfavour. I have attempted a solution given on stackoverflow such as the one below:
conditions <- c(rep("WTNaClctrl", 1229), 
               rep("WTBlu", 1328),
               rep("ILKOBlu", 349))


conditions <- as.data.frame(conditions)

seurat_object@meta.data$conditions <- factor(conditions$conditions, levels=unique(conditions$conditions))

When I use the FeatureHeatmap() function, the plot still sorts the data groups in alphabetical order:
FeatureHeatmap(seurat_object, features.plot = c("Gene1", "Gene2"), 
               pt.size = 0.25,  
               group.by = "conditions",
               max.exp = 3)

This plot gets produced: 
I would like groupings not to appear in alphabetical order as such:

As previously suggested StackExchange, I have tried to upgrade my Seurat object to Seurat V3:
UpdateSeuratObject(Seurat_object): 
Object representation is consistent with the most current Seurat version.

devtools::install_github(repo = 'satijalab/seurat', ref = 'release/3.0') : package ‘curl’ successfully unpacked and MD5 sums checked: 

Error: (converted from warning) cannot remove prior installation of package ‘curl’.

Thank you in advance for any suggestions.

**Tags:** r, scrnaseq, seurat, ggplot2
> **Comments:**
> **username_1**: The error you got is from not having the rights to update curl package. Maybe it was installed by the administrator of the computer (sudo?). The plotting side of seurat seems to be done with ggplot2, which orders the categoric values based on the levels of the factors. Reorder the levels and it should go as you wish (I'd recommend `forcats`)  
> **username_0**: @IIrs Please I am not clear about the point in the code where I should apply `forcats`.  
> **username_1**: forcats is a package designed to make it easier to work with factors, it provides some functions to reorder the levels of the factors. I don't know seurat good enough to suggest you where you should use it.  
> **username_2**: Possible duplicate of [How to set the position of groups in a Seurat object on a FeatureHeatmap plot](https://bioinformatics.stackexchange.com/questions/6838/how-to-set-the-position-of-groups-in-a-seurat-object-on-a-featureheatmap-plot)  
> **username_0**: @TimStuart: I think in the other question, the alphabetical impact is not highlighted. So I thought of making it a new question.  
> **username_2**: As I said in your previous question, you can't change the order using Seurat v2. The groups are coerced to a character vector which is then ordered alphabetically by ggplot2. You can either use Seurat 3 and reorder the factors, or extract the data from the object and plot it yourself  
> **username_0**: @TimStuart: In the question I also highlighted my difficulty in automatically downloading the Seurat V3 package due to admin rights issues. Could I please have a link where I could download the package and load it manually?  
> **username_2**: See the github page here: https://github.com/satijalab/seurat/tree/release/3.0 and the website here: https://satijalab.org/seurat/install.html#prerelease  

---

## 1 answers

### Answer by username_3 on 2019-03-23. Score: 1

I had this same issue. I reordered the levels and that worked for most functions, but would switch back with the FeatureHeatmap. Turns out there is a line in the function that changes it to a character. 
I ended up making a "new" function and just changed that one line from:
data.plot$ident <- as.character(x = object@ident)

to:
data.plot$ident <- object@ident

Once that was fixed, the order that I assigned worked out just fine.


---

