Search code examples
rggplot2seurat

How to reorder cells in DoHeatmap plot in Seurat (ggplot2)


I'm plotting a heatmap with Seurat in R

require(Seurat)
data <- data.frame(cell1=c(-0.5, 0.5), cell2=c(-0.8, 0.3), cell3=c(2.0, 0.1), cell4=c(1.0, 1.0))
rownames(data) <- c("gene1", "gene2")
test <- CreateSeuratObject(data)
[email protected] <- data
DoHeatmap(test)

And this is what I get

heatmap result

I want to reorder cells here with custom order. I have tried DoHeatmap(test, data.use=<reordered data>) and p <- DoHeatmap(…, plot=FALSE) and then reordering p$data, but to no avail


Solution

  • We can coerce p$data$cell to a factor and specify the levels according to our needs.

    set.seed(1)
    (custom_order <- paste0("cell", sample(4)))
    #[1] "cell2" "cell4" "cell3" "cell1"
    

    Create the plot and reorder

    p <- DoHeatmap(test)
    p$data$cell <- factor(p$data$cell, levels = custom_order)
    p
    

    enter image description here