gpt4 book ai didi

r - 如何在 R 中为绘图热图生成自定义色标

转载 作者:行者123 更新时间:2023-12-01 12:20:15 24 4
gpt4 key购买 nike

我想获得一个自定义色标,它看起来像 plotly 热图( 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,绘制的数据在这个区间会有不同的值。

最佳答案

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

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

这给出了以下热图:

enter image description here
## 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")

产生这个 plotly :
enter image description here

请让我知道这是否是您想要的。

更新

您可以在 plot_ly 中设置自定义比例与 zauto , zmax , 和 zmin .以下 2 段代码和图表将说明这一点:

比例设置为 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)

enter image description here

比例设置为 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)

enter image description here

关于r - 如何在 R 中为绘图热图生成自定义色标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44918709/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com