welch-lab / liger

R package for integrating and analyzing multiple single-cell datasets
GNU General Public License v3.0
391 stars 78 forks source link

setting axis limits with plotGeneDimRed #320

Closed jessegmeyerlab closed 1 month ago

jessegmeyerlab commented 3 months ago

I'm getting a group of cells at an extreme value of y that is only in one group and when I plot with "plotGeneDimRed" it makes it so the plot axes limits are not comparable and therefore hard to compare markers across. How can I set the axis ranges for this plotting? image

mvfki commented 3 months ago

Hi,

Unfortunately, the axis limit setting is not built-in in LIGER's plotting function. However, the scatter plot functions are all built upon ggplot2 so you can easily make the modification on your own after the plotGeneDimRed() function returns you the plot.

I assume what you shew there is done by setting `splitBy = "dataset", which makes scatter plot basing on each individual dataset (that's why axes are different for subplots), and then using some subplot combining function to combine them. I agree that having globally consistent axis limits is helpful for comparison across groups.

Here's the work-around I can show you to manually resolve the problem. You'll want to keep the list that plotGeneDimRed() returned and start from there:

plotList <- plotGeneDimRed(obj, "CNN1", splitBy = "dataset")

Each subplot can be accessed with plotList[["CNN1.nameOfDataset"]].

Then determine the range you want to manually set to both, which can be easily obtained with the following code chunk. I'm not sure if you have more than these two to be considered, so please note that thess ranges are for the whole integrated UMAP of all your datasets/groups.

globalXRange <- range(dimRed(obj, "UMAP")[,1])
globalYRange <- range(dimRed(obj, "UMAP")[,2])

Next, some shortcut syntax to apply the same change to all elements of a list object.

library(ggplot2)
plotListUpdated <- lapply(plotList, function(p) {
    p + scale_x_continuous(limits = globalXRange) + scale_y_continuous(limits = globalYRange)
})

Lastly, combine them back manually with your preferred tool:

library(cowplot)
plot_grid(plotlist = plotListUpdated)