title: Plotting a clustered heatmap with dendrograms using R's plotly
body: <p>I'm following <a href="https://plot.ly/ggplot2/ggdendro-dendrograms/" rel="noreferrer">this example</a> on how to create a clustered heatmap with dendrograms with <code>R</code>'s <code>plotly</code>. Here's the example:</p>
  
  <pre><code>library(ggplot2)
  library(ggdendro)
  library(plotly)
  
  #dendogram data
  x &lt;- as.matrix(scale(mtcars))
  dd.col &lt;- as.dendrogram(hclust(dist(x)))
  dd.row &lt;- as.dendrogram(hclust(dist(t(x))))
  dx &lt;- dendro_data(dd.row)
  dy &lt;- dendro_data(dd.col)
  
  # helper function for creating dendograms
  ggdend &lt;- function(df) {
    ggplot() +
      geom_segment(data = df, aes(x=x, y=y, xend=xend, yend=yend)) +
      labs(x = "", y = "") + theme_minimal() +
      theme(axis.text = element_blank(), axis.ticks = element_blank(),
            panel.grid = element_blank())
  }
  
  # x/y dendograms
  px &lt;- ggdend(dx$segments)
  py &lt;- ggdend(dy$segments) + coord_flip()
  
  # heatmap
  col.ord &lt;- order.dendrogram(dd.col)
  row.ord &lt;- order.dendrogram(dd.row)
  xx &lt;- scale(mtcars)[col.ord, row.ord]
  xx_names &lt;- attr(xx, "dimnames")
  df &lt;- as.data.frame(xx)
  colnames(df) &lt;- xx_names[[2]]
  df$car &lt;- xx_names[[1]]
  df$car &lt;- with(df, factor(car, levels=car, ordered=TRUE))
  mdf &lt;- reshape2::melt(df, id.vars="car")
  p &lt;- ggplot(mdf, aes(x = variable, y = car)) + geom_tile(aes(fill = value))
  
  mat &lt;- matrix(unlist(dplyr::select(df,-car)),nrow=nrow(df))
  colnames(mat) &lt;- colnames(df)[1:ncol(df)-1]
  rownames(mat) &lt;- rownames(df)
  
  # hide axis ticks and grid lines
  eaxis &lt;- list(
    showticklabels = FALSE,
    showgrid = FALSE,
    zeroline = FALSE
  )
  
  p_empty &lt;- plot_ly(filename="r-docs/dendrogram") %&gt;%
    # note that margin applies to entire plot, so we can
    # add it here to make tick labels more readable
    layout(margin = list(l = 200),
           xaxis = eaxis,
           yaxis = eaxis)
  
  subplot(px, p_empty, p, py, nrows = 2, margin = 0.01)
  </code></pre>
  
  <p>which gives:</p>
  
  <p><a href="https://i.stack.imgur.com/vtZkM.png" rel="noreferrer"><img src="https://i.stack.imgur.com/vtZkM.png" alt="enter image description here"></a></p>
  
  <p>I changed the code a bit so that in my case the heatmap is generated with <code>plotly</code> rather than <code>ggplot</code> since it runs faster on my real big data, hence I do:</p>
  
  <pre><code>heatmap.plotly &lt;- plot_ly() %&gt;% add_heatmap(z=~mat,x=factor(colnames(mat),lev=colnames(mat)),y=factor(rownames(mat),lev=rownames(mat)))
  </code></pre>
  
  <p>And then:</p>
  
  <pre><code>subplot(px, p_empty, heatmap.plotly, py, nrows = 2, margin = 0.01)
  </code></pre>
  
  <p>which gives:
  <a href="https://i.stack.imgur.com/SHB4q.png" rel="noreferrer"><img src="https://i.stack.imgur.com/SHB4q.png" alt="enter image description here"></a></p>
  
  <p>My questions are:</p>
  
  <ol>
  <li><p>How do I get the row and column labels of the heatmap not get cut off as they do in both plots?</p></li>
  <li><p>The label of the colorer is changed to "mat" in the second figure. Any idea how to prevent that?</p></li>
  <li><p>How do I change the margins between the heatmap and the dendrograms?</p></li>
  </ol>