This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Re-arrange conditions in Seurat

Hello,

Would anyone know how to re-arrange the output of featureplots/dimplots when using the 'split.by = 'stim'' function?

I have 3 conditions, Young, Adult and Old but the output of the featureplot panels is Old, Young, Adult. Tried to re-arrange them using "levels" but ended up breaking the Seurat object. oups.

Any suggestions are very helpful. Thanks

seurat rnaseq

Hi,

Can you post the code that you've used here?

The Seurat makes use of ggplot2 R package. So, the best way of change it is probably changing the data that is returned by the feature plot and plot it again. I can try to help, but it would be better with a code example.

António

I think it has something to do with how they are ordered in Seurat rather than the way ggplot2 is plotting it. They code I use to generate it is :

DimPlot(SeuratObject, split.by = "stim")

I'll add my comment as an answer since I believe it will solve your problem. Usually, plots, labels etc are order by alphabetical order.

António

Using levels is usually the correct way to handle this. Can you elaborate as to what you mean when you say you broke the Seurat object?

I'm not exactly sure I can re-collect from memory. What I believe I did was apply levels to the SeuratObject$data but then even calling simple things like DimPlot did not work.

The solution posted below by antonio seems to work but would there be a way to properly apply levels to the seurat object so that DimPlot, featureplot etc all plot that order by default? Rather than having to apply levels to each plot individually.

1 answer

Hi,

So, I follow the tutorial of Seurat until the this function:

DimPlot(immune.combined, reduction = "umap", split.by = "stim")

Then I save the result of the previous function into a variable like:

dim_plot <- DimPlot(immune.combined, reduction = "umap", split.by = "stim")

Now, if you check the class(dim_plot) it will retrieve a object class ggplot. In sum, this dimplot is a ggplot plot. Since ggplot class objects are a class that save the data frame used to plot them in the same object, you can just change the levels that you want to plot there and print again the plot. So, in this example the data frame of the dim_plot object is save in dim_plot$data

So, if you do head(dim_plot$data) - object that holds the data used to plot, you'll see something like this:

                UMAP_1      UMAP_2 ident stim
AAACATACATTTCC.1  8.229279  -3.3894200     0 CTRL
AAACATACCAGAAA.1  6.816155   0.7878036     0 CTRL
AAACATACCTCGCT.1  7.293071  -2.7933776     0 CTRL
AAACATACCTGGTA.1  3.915340 -12.0176646    11 CTRL
AAACATACGATGAA.1 -7.483353   5.3328951     2 CTRL
AAACATACGGCATT.1  4.863862  -3.6870772     0 CTRL
  

So, to change the order of factor levels, I need to change the order of the levels of the factor column stim, by doing:

dim_plot$data$stim <- factor(x = dim_plot$data$stim, levels = c("STIM", "CTRL")) # change the order of the factor levels
dim_plot # print again the plot

Instead of plotting first the "CTRL" condition and then the "STIM", it will plot first the "STIM", and only then "CTRL". Be aware that it perhaps exist other solution rather than this, but this will work just fine.

In your case you can try something like:

dim_plot <- DimPlot(SeuratObject, split.by = "stim")

dim_plot$data$stim <- factor(x = dim_plot$data$stim, levels = c("Young", "Adult", "Old")) # change the order of the factor levels

dim_plot # print again the plot

I hope this answers your question,

António

this seems to solve it, but would you know how this could be done to the Seurat object so it plots it in a specific order by default?

This may work:

SeuratObject$stim <- factor(SeuratObject$stim, levels = c("Young", "Adult", "Old"))
DimPlot(SeuratObject, split.by = "stim")

Try it and let me know if worked.

António

that's amazing. It did. Thank you :)

Log in to answer this question.