Search code examples
rheatmapaxesr-plotly

Categorical axes in plotly heatmapgl


Hi I've some problems with heatmapgl plot (but with heatmap I get the same) of plotly package. I've a distance matrix between a series of elements. I would like to plot a heatmap with names of elements on axes and possibly also on hover text labels.

library(plotly)
values=c(22.63409, 323.93780, 366.31498, 467.94524, 461.93251, 643.61306, 665.03471, 482.49047, 500.06208, 662.71600, 673.29995, 567.99427, 558.41419, 632.86704, 615.55000, 508.01659, 462.50232, 411.39160, 399.00598, 286.55681, 268.69871) 
    z_matrix=matrix(NA,7,7)
    z_matrix[lower.tri(z_matrix, diag=FALSE)] <- values
    elements_names=c("ATHOTPlun04", "ATHOTPlun10", "ATHOTPlun16", "ATHOTPlun22", "ATHOTPmar04", "ATHOTPmar10", "ATHOTPmar16")
q <- plot_ly(
    x=elements_names,
    y=elements_names,
    z = z_matrix, type = "heatmapgl") %>%
    layout(title = 'Distances for ATHOTP route')
q

Although this is what I get when I run the code: enter image description here

I tried also to set axes type to "category" in layout options but nothing seems to change. What am I missing? Thank you!


Solution

  • heatmap-gl is used for images where you have continuous x- and y-axes. Therefore you need to change type to a non-continuous type, e.g. type = "heatmap".

    q <- plot_ly(  x=elements_names,
                   y=elements_names,
                   z = z_matrix, type = "heatmap") %>%
      layout(title = 'Distances for ATHOTP route')
    q