talgalili / heatmaply

Interactive Heat Maps for R Using plotly
378 stars 73 forks source link

Heatmaply and heatmap.2 dendrograms don't seem to match when scaling is used #245

Open simonroome opened 4 years ago

simonroome commented 4 years ago

Thank you again for the great package.

I was having trouble getting the heatmaply dendrograms to match the heatmap.2 dendrogram, when I came across the heatmaply vignette directions to make them match. It seemed to work when scaling wasn't applied, but when I tried to apply row or column scaling, they no longer matched. Applying the scaling didn't seem to change the heatmap.2 dendrogram, but it did change the heatmaply dendrogram.

I made a quick shiny app to show the behaviour I'm getting with the scaling.

library(shiny)

ui <- fluidPage(
  plotlyOutput("heatmap"),
  plotOutput("heatmap2", height = "1000px")
)

server <- function(input, output, session) {

  x  <- as.matrix(datasets::mtcars)

  output$heatmap <- renderPlotly({
    heatmaply(x,

#### setting scale to "column" here instead of "none" causes the dendrogram to change for heatmaply

              scale = "column",
              seriate = "mean",
              row_dend_left = T,
              plot_method = "plotly")
  })
  output$heatmap2 <- renderPlot({
    heatmap(x,

#### setting scale to "column" here instead of "none" doesn't seem to change the dendrogram in heatmap.2
#### so if both heatmaply and heatmap.2 are set to "column", only the heatmaply dendrogram changes and 
#### they no longer match

              scale = "column", 
              trace = "none",
              col = viridis(100),
              key = T)
  })
}

shinyApp(ui, server)

I would expect that since the settings selected are those that in the vignette made heatmaply and heatmap.2 dendrograms match for non-scaled data, that the dendrograms would also match when you apply column scaling to both. There might be a very simple explanation for this that I'm missing or misunderstanding. I'm not sure if this is expected behaviour and if it might be better to post this on StackOverflow or if it is unexpected and this is the appropriate place to post this - if it's expected I apologize.

alanocallaghan commented 4 years ago

Scaling in heatmap.2 is applied after calculating dendrograms, in heatmaply scaling happens before. I prefer the heatmaply behaviour as otherwise there's a mismatch in the data and clustering. You can get the heatmap.2 behaviour by calculating dendrograms on raw data and passing them in while specifying the desire scale setting