3
$\begingroup$

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)

[email protected]$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

I would like groupings not to appear in alphabetical order as such: preferred order

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.

$\endgroup$
8
  • $\begingroup$ 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) $\endgroup$
    – llrs
    Commented Jan 21, 2019 at 10:38
  • $\begingroup$ @IIrs Please I am not clear about the point in the code where I should apply forcats. $\endgroup$
    – Charles
    Commented Jan 21, 2019 at 10:45
  • 5
    $\begingroup$ Possible duplicate of How to set the position of groups in a Seurat object on a FeatureHeatmap plot $\endgroup$
    – TimStuart
    Commented Jan 21, 2019 at 14:35
  • 1
    $\begingroup$ 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 $\endgroup$
    – TimStuart
    Commented Jan 21, 2019 at 15:57
  • 1
    $\begingroup$ See the github page here: github.com/satijalab/seurat/tree/release/3.0 and the website here: satijalab.org/seurat/install.html#prerelease $\endgroup$
    – TimStuart
    Commented Jan 21, 2019 at 16:39

1 Answer 1

1
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.