Using discrete custom color in a plotly heatmap

后端 未结 4 577
暖寄归人
暖寄归人 2020-12-18 06:28

I\'m trying to generate a plotly heatmap, where I\'d like the colors to be specified by a discrete scale.

Here\'s what I mean:

Gene

4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-18 06:31

    Let's get a discrete colorscale

    df_colors = data.frame(range=c(0:11), colors=c(0:11))
    color_s <- setNames(data.frame(df_colors$range, df_colors$colors), NULL)
    for (i in 1:12) {
      color_s[[2]][[i]] <- interval.cols[[(i + 1) / 2]]
      color_s[[1]][[i]] <-  i / 12 - (i %% 2) / 12
    }
    

    And get a nice colorbar by setting ticktext and squeezing it (len=0.2)

    colorbar=list(tickmode='array', tickvals=c(1:6), ticktext=levels(mat.intervals), len=0.2)
    

    All the code which needs to be added to your example

    df_colors = data.frame(range=c(0:11), colors=c(0:11))
    color_s <- setNames(data.frame(df_colors$range, df_colors$colors), NULL)
    
    for (i in 1:12) {
      color_s[[2]][[i]] <- interval.cols[[(i + 1) / 2]]
      color_s[[1]][[i]] <-  i / 12 - (i %% 2) / 12
    }
    
    
    plot_ly(z=c(interval.df$expr), x=interval.df$sample, y=interval.df$gene, colorscale = color_s, type = "heatmap", hoverinfo = "x+y+z", colorbar=list(tickmode='array', tickvals=c(1:6), ticktext=levels(mat.intervals), len=0.2))
    

提交回复
热议问题