Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-03-23T01:32:39.607Z
Answers.LastActivityDate: 2019-03-23T01:41:40.603Z
Answers.LastEditDate: 2019-03-23T01:41:40.603Z
Answers.UserDisplayName: Amber
Body: <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>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-01-21T10:38:35.270Z
Comments.Text: 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`)
Comments.UserDisplayName: llrs
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-01-21T10:45:58.040Z
Comments.Text: @IIrs Please I am not clear about the point in the code where I should apply `forcats`.
Comments.UserDisplayName: Charles
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-01-21T14:29:32.703Z
Comments.Text: 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.
Comments.UserDisplayName: llrs
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-01-21T14:35:39.683Z
Comments.Text: 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)
Comments.UserDisplayName: TimStuart
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-01-21T15:26:55.107Z
Comments.Text: @TimStuart: I think in the other question, the alphabetical impact is not highlighted. So I thought of making it a new question.
Comments.UserDisplayName: Charles
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-01-21T15:57:13.330Z
Comments.Text: 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
Comments.UserDisplayName: TimStuart
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-01-21T16:33:12.377Z
Comments.Text: @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?
Comments.UserDisplayName: Charles
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-01-21T16:39:12.547Z
Comments.Text: 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
Comments.UserDisplayName: TimStuart
Tags: <r><scrnaseq><seurat><ggplot2>
Title: How do I prevent the FeatureHeatmap function from the Seurat package, from sorting my data groups in alphabetical order when plotting data?
ThreadText: # 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](https://stackoverflow.com/questions/16897043/prevent-sorting-of-factor-levels) 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: [![in alphabetical order](https://i.sstatic.net/4WCei.png)](https://i.sstatic.net/4WCei.png)
  
  I would like groupings not to appear in alphabetical order as such:
  [![preferred order](https://i.sstatic.net/6bmOv.png)](https://i.sstatic.net/6bmOv.png)
  
  As previously suggested [StackExchange](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), 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**: @username_2: 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**: @username_2: 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.
  
  
  ---
  
  