Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

split.by variable independent from colour variable #4

Closed
Dario-Rocha opened this issue Oct 22, 2022 · 4 comments
Closed

split.by variable independent from colour variable #4

Dario-Rocha opened this issue Oct 22, 2022 · 4 comments
Labels
enhancement New feature or request

Comments

@Dario-Rocha
Copy link

The package is amazing, but I can't find a way to split plots by a desired categorical variable different than the categorical variable that is used to colour the plot in do_DimPlot, do_RidgePlot and do_ViolinPlot. I also am unable to split the do_NebulosaPlot plot by a categorical variable.

For do_DimPlot, I am trying to colour cells by cluster and separate the plot in two by sample. For example, in base seurat the resulting plot from

DimPlot(seurat.object,
           group.by = 'seurat_clusters',
           split.by = 'orig.ident')

image

When trying to do the same with do_DimPlot

do_DimPlot(seurat.object,
           group.by = 'seurat_clusters',
           split.by = 'orig.ident')

returns
Error: Either group.by or split.by has to be NULL.

do_FeaturePlot, instead, allows the colouring variable to be different from the splitting variable, although the colouring variable can't be categorical.

In a similar fashion, for ridge and violin plots, I'd like to include two categorical variables. For a basic seurat violin plot, this can be achieved with

VlnPlot(object = seurat.object,
        features = 'feature',
        split.by = 'orig.ident',
        group.by = 'seurat_clusters')

image
but I am unable to find a wat to do it with SCPubr

Hopefully there is a way to do this because I am in love with this package aesthetics

@enblacar
Copy link
Owner

Hi @Dario-Rocha,

Thanks for using the package! I am happy you find it useful and you like it!

Regarding your request, I recently received an email with the exact same issue. As of now, the use of group.by and split.by is deliberately censored by the function, as the current implementation of split.by uses cells.highlight as base to "color" the cells. However, given that there is a real interest in this kind of use case, I will give it some work for the next update! It might take a bit of time, though! I will also have a look to the violin/ridge plots, that one should be fairly easy to implement.

I will update this thread once the package is updated on CRAN! Thanks a lot for the ideas!

Best,
Enrique

@enblacar enblacar added the enhancement New feature or request label Oct 22, 2022
@Dario-Rocha
Copy link
Author

Thank you so much for your prompt reply,
If it is of any help, I've sort of achieved what I was looking for by making two separate plots and manually setting to NA the cells that I want to be greyed out, for example:

seurat.object$seurat_clusters[seurat.object $orig.ident=='tumor'] <- NA
do_DimPlot(sample = seurat.object,
                               group.by = 'seurat_clusters',
                               shuffle = FALSE)

And then doing the same for the other levels of orig.ident. However, I can't get to consistently push all the NA cells to the background of the plot, i.e.: all cells with a value different from NA should always be plotted on top of cells with NA value.
When turning shuffle off, this is the result:
image

For some reason that I can't figure out, when turning shuffle off for another subset of the orig.ident factor, the behaviour is closer to what I desire (i.e.: coloured cells on top of grey cells)

seurat.object$seurat_clusters[seurat.object $orig.ident=='tumor'] <- NA
do_DimPlot(sample = seurat.object,
                               group.by = 'seurat_clusters',
                               shuffle = FALSE)

image

Ideally, coloured cells should be shuffled between them, and NA cells should be pushed to the back of the plot.

@enblacar
Copy link
Owner

enblacar commented Oct 23, 2022

Hi @Dario-Rocha,

Thanks for giving it a go! Indeed, I was also following a similar approach and also faced the NA issue. I was able to solve it by:

  • Generating a new point layer, with all the cells colored the same as na.value.
  • Then, subsetting the Seurat object to the cells in each iteration of split.by, then computing the DimPlot only for those cells and plotting that on top. That should allow for the correct ordering/shuffling of the values on top of the grey NA cells.
  • Then, all individual plots are pulled together using patchwork.

I have tested this new implementation and it should not break. However, it would be nice if you could also give it a go in your dataset and tell me how it looks! For this, please install:

devtools::install_github("enblacar/SCpubr", ref = "59895b45ff3b412d7e467a12146acec73c797c81")

And run:

SCpubr::do_DimPlot(sample = sample, group.by = "seurat_clusters", split.by = "orig.ident")

Or use the variables of your choice. Other parameters such as label, legend.position, plot_cell_borders should also work as expected!

In this commit, I also added the split.by functionality to SCpubr::do_ViolinPlot(). Would you mind also giving it a go? I could not manage to add the boxplots when split.by is set, they did not align properly. However, this should achieve what you were looking for!

For SCpubr::do_NebulosaPlot(), I would actually refrain from splitting by a categorical variable, as it computes the densities based on the surrounding cells (therefore, after splitting it, the values will no longer be true with regards to the different cells). Since I am not 100% on top of the methodology behind how the densities are computed, I think I will leave it as is for the time being, as also the developers did not include a similar functionality in Nebulosa.

For SCpubr::do_RidgePlot(), it actually has a split.by parameter, that will facet the plot according to the values. Is this what you were looking for?

Finally, please remember that you are downloading a developmental version of the package. Treat it as if anything could break any moment. Once you have your desired plot, please install back the CRAN version and wait for the official update to be released!

Looking forward for your feedback!
Enrique

@enblacar
Copy link
Owner

Hi @Dario-Rocha,

I am closing this issue as I believe all the necessary changes have been implemented in SCpubr v1.0.2.

Please update the package from CRAN and if some issues persist, feel free to open another Issue!

Thanks for your feedback!
Enrique

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants