text: user3968: 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.
  
  user4304: 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.
  
original_text: 3968: <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.stack.imgur.com/4WCei.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/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.stack.imgur.com/6bmOv.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/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>
   4304: <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>
  