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

If scale=FALSE, DotPlot logs data such that we get negative values #4298

Closed
ktrns opened this issue Mar 29, 2021 · 2 comments
Closed

If scale=FALSE, DotPlot logs data such that we get negative values #4298

ktrns opened this issue Mar 29, 2021 · 2 comments
Labels
bug Something isn't working

Comments

@ktrns
Copy link

ktrns commented Mar 29, 2021

Hi there,

You use log1p to LogNorm counts. In the Dotplot function, you use the normalised data, and unlog it to compute an average:

    data.plot <- lapply(X = unique(x = data.features$id), FUN = function(ident) {
        data.use <- data.features[data.features$id == ident, 
            1:(ncol(x = data.features) - 1), drop = FALSE]
        avg.exp <- apply(X = data.use, MARGIN = 2, FUN = function(x) {
            return(mean(x = expm1(x = x)))
        })
        pct.exp <- apply(X = data.use, MARGIN = 2, FUN = PercentAbove, 
            threshold = 0)
        return(list(avg.exp = avg.exp, pct.exp = pct.exp))
    })

And then, in the case of scale=FALSE, you log the averaged data again:

   avg.exp.scaled <- sapply(X = unique(x = data.plot$features.plot), 
        FUN = function(x) {
            data.use <- data.plot[data.plot$features.plot == 
                x, "avg.exp"]
            if (scale) {
                data.use <- scale(x = data.use)
                data.use <- MinMax(data = data.use, min = col.min, 
                  max = col.max)
            }
            else {
                data.use <- log(x = data.use)
            }
            return(data.use)
        })

However, we end up with lots of negative values and this is misleading. Can you switch to using log1p here as well?

Thank you,
Katrin

@ktrns ktrns added the bug Something isn't working label Mar 29, 2021
@liu-xingliang
Copy link

Log scale implies negative values (original value < 1). Personally, from a software developer view, here for scale = FALSE, using log or log1p both make sense (of course, 0s in data.use should be taken care of). From a user view, I think a more informative legend title helps.

Waiting for development team, before that, a more add-hoc way could be get data.frame behind Seurat::DotPlot and re-generate using ggplot2::geom_point():

library(Seurat) # Seurat_3.1.5

dotplot.gg_data <- DotPlot(
    object,
    features = features
)$data

# Using scaled average expression (Z-score)
g.scaled <- ggplot(data = dotplot.gg_data, mapping = aes(x = id, y = features.plot)) +
    geom_point(mapping=aes(size = pct.exp, color = avg.exp.scaled)) +
    scale_radius(range = c(0, 6)) +
    labs(
         title = "Title",
         subtitle = "Sub-title",
         x = "Identity",
         y = "Feature"
     ) +
     guides(
         size = guide_legend(title = "Expression Percentage"),
         color = guide_colourbar(title = "Scaled Average Expression")
     ) +
     cowplot::theme_cowplot()

# log1p
g.unscaled_log1p <- ggplot(data = dotplot.gg_data, mapping = aes(x = id, y = features.plot)) +
    geom_point(mapping=aes(size = pct.exp, color = log1p(avg.exp))) +
    scale_radius(range = c(0, 6)) +
    labs(
         title = "Title",
         subtitle = "Sub-title",
         x = "Identity",
         y = "Feature"
     ) +
     guides(
         size = guide_legend(title = "Expression Percentage"),
         color = guide_colourbar(title = "Log(Average Expression + 1)")
     ) +
     cowplot::theme_cowplot()

# log
g.unscaled_log <- ggplot(data = dotplot.gg_data, mapping = aes(x = id, y = features.plot)) +
    geom_point(mapping=aes(size = pct.exp, color = log(avg.exp))) +
    scale_radius(range = c(0, 6)) +
    labs(
         title = "Title",
         subtitle = "Sub-title",
         x = "Identity",
         y = "Feature"
     ) +
     guides(
         size = guide_legend(title = "Expression Percentage"),
         color = guide_colourbar(title = "Log(Average Expression)")
     ) +
     cowplot::theme_cowplot()

@timoast
Copy link
Collaborator

timoast commented Apr 23, 2021

This has now been fixed by @saketkc on the develop branch

@timoast timoast closed this as completed Apr 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants