简体   繁体   English

如何为R中的绘图热图生成自定义色标

[英]How to generate a custom color scale for plotly heatmap in R

I would like to get a custom color scale which looks like for plotly heatmap ( plot_ly(z = data, colors = customcolors, type = "heatmap") ) 我想获得一个自定义的色阶,看起来像是可绘制的热图( plot_ly(z = data, colors = customcolors, type = "heatmap")

palette <- colorRampPalette(c("darkblue", "blue", "lightblue1",
                          "green","yellow", "red", "darkred"))
plot(rep(1,50),col=palette(50), pch=19, cex=3, xlab = "", ylab ="", axes = F)

and where the blue end represents 1 and red end represents 10^6 and plotted data would have varying values on this interval. 而蓝色端代表1,红色端代表10 ^ 6,并且绘制的数据在此间隔上具有变化的值。

The code you use to generate a color palette works just fine. 您用于生成调色板的代码可以正常工作。 You only need to provide data that match with a heatmap . 您只需要提供与heatmap匹配的数据即可。 The following code provides this: 以下代码提供了这一点:

library(RColorBrewer)
library(plotly)

# your palette definition
palette <- colorRampPalette(c("darkblue", "blue", "lightblue1",
                              "green","yellow", "red", "darkred"))

set.seed(9876)    # for reproducibility

## a complete random set
hmdata <- matrix(data = sample(x = 1:10^6, size = 100*100), nrow = 100, ncol = 100)
plot_ly(z = hmdata, colors = palette(50), type = "heatmap")

This gives the following heatmap: 这给出了以下热图:

在此处输入图片说明

## a random set that has been sorted
hmdata_s <- matrix(data = sort(sample(x = 1:10^6, size = 100*100)), nrow = 100, ncol = 100)
plot_ly(z = hmdata_s, colors = palette(50), type = "heatmap")

Yielding this plot: 产生此图: 在此处输入图片说明

Please let me know whether this is what you want. 请让我知道这是否是您想要的。

UPDATE UPDATE

you can set custom scale in plot_ly with zauto , zmax , and zmin . 您可以设置自定义规模plot_lyzautozmaxzmin The following 2 pieces of code and graphs will illustrate this: 以下2段代码和图表将说明这一点:

The scale is set from 1 to 100 and the data vary similarly: 比例尺设置为1到100,数据的变化类似:

hmdata_s3 <- matrix(data = sort(sample(x = 1:100, size = 100*100, replace = TRUE)), nrow = 100, ncol = 100)
plot_ly(z = hmdata_s3, colors = palette(50), type = "heatmap", zauto = FALSE, zmin = 1, zmax = 100)

在此处输入图片说明

The scale is set from 1 to 100 and the data vary between 50 and 100 only 小数位设置为1到100,数据仅在50到100之间变化

hmdata_s4 <- matrix(data = sort(sample(x = 50:100, size = 100*100, replace = TRUE)), nrow = 100, ncol = 100)
plot_ly(z = hmdata_s4, colors = palette(50), type = "heatmap", zauto = FALSE, zmin = 1, zmax = 100)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM