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

How to change plotting order in plot_dimred, as in Seurat? #63

Closed
erzakiev opened this issue Oct 4, 2023 · 1 comment
Closed

How to change plotting order in plot_dimred, as in Seurat? #63

erzakiev opened this issue Oct 4, 2023 · 1 comment

Comments

@erzakiev
Copy link

erzakiev commented Oct 4, 2023

I want to change the plotting order in accordance to the expression level of a certain gene/set of genes:
cells expressing a certain gene at a higher level are plotted on top of those expressing it at a lower level.

Because I see that with the current plotting scheme the cells with low expression are often plotted above the cells with high expression of a certain gene.

In Seurat, for instance, the plotting order can be controlled with an argument called order

Seurat::FeaturePlot(x, "feature", cols = c("grey90", viridis::inferno(10000, begin = 0, end = 1, direction = -1)), reduction = "pca", dims = c(2,3))

When order=F
image

When ordering is to be done:
image

For the ggplot2, the basis of our plot_dimred, I found this post and tried both answers, none worked.

Reproducible example:

library(dyno)
smol <- readRDS(url('https://drive.google.com/u/0/uc?id=1ZjMOSiF2BULWIO4A5Ki58jGDfyMEXYAF&export=download'))
plot_dimred(smol, feature_oi = 'T_signature_gene')

image

@erzakiev
Copy link
Author

erzakiev commented Sep 8, 2024

i've since devised a function that rearranges BOTH trajectory and dimensional reduction, and that works:

ArrangeDataSetAccordingToFeature <- function(dataset, ordr){
  if(is.null(ordr)) return(dataset)
  if("dynwrap::with_expression" %in% class(dataset)){
    # if dataset is a dynwrap trajectory object
    dataset$cell_info <- dataset$cell_info[ordr,]
    dataset$grouping <- dataset$grouping[ordr]
    dataset$expression <- dataset$expression[ordr,]
    dataset$counts <- dataset$counts[ordr,]
    dataset$cell_ids <- dataset$cell_ids[ordr]
  } else {
    # dataset is dimensional reduction
    dataset <- dataset[ordr,]
  }
  return(dataset)
}

# the order of the feature in question
ord <- order(trajectory$expression[,feature], decreasing = F, na.last = F)

# changing the order for both the trajectory and the dimensional reduction
plot_dimred(ArrangeDataSetAccordingToFeature(trajectory, ord),
                    size_cells = 0.5,
                    dimred = ArrangeDataSetAccordingToFeature(dr, ord), 
                    feature_oi = feature,
                    plot_trajectory = F, 
                    hex_cells = F, 
                    color_cells = 'feature')

@erzakiev erzakiev closed this as completed Sep 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant