如何在 R 中为 plotly 热图生成自定义色标 [英] How to generate a custom color scale for plotly heatmap in R

查看:29
本文介绍了如何在 R 中为 plotly 热图生成自定义色标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得一个自定义色标,看起来像 plotly heatmap (plot_ly(z = data, colors = customcolors, type = "heatmap"))

I would like to get a custom color scale which looks like for plotly 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)

其中蓝色端代表 1,红色端代表 10^6,并且绘制的数据在此区间内会有不同的值.

and where the blue end represents 1 and red end represents 10^6 and plotted data would have varying values on this interval.

推荐答案

用于生成调色板的代码可以正常工作.您只需提供与 heatmap 匹配的数据.以下代码提供了这一点:

The code you use to generate a color palette works just fine. You only need to provide data that match with a 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")

这给出了以下热图:

## 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")

产生这个情节:

请告诉我这是否是你想要的.

Please let me know whether this is what you want.

您可以使用 zautozmaxzminplot_ly 中设置自定义比例.以下 2 段代码和图表将说明这一点:

you can set custom scale in plot_ly with zauto, zmax, and zmin. The following 2 pieces of code and graphs will illustrate this:

比例设置为 1 到 100,数据变化类似:

The scale is set from 1 to 100 and the data vary similarly:

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)

比例设置为 1 到 100,数据仅在 50 到 100 之间变化

The scale is set from 1 to 100 and the data vary between 50 and 100 only

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)

这篇关于如何在 R 中为 plotly 热图生成自定义色标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆