在绘图热图中指定颜色 [英] Specifying the colors in a Plotly Heatmap

查看:756
本文介绍了在绘图热图中指定颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个带有分类轴的Plotly热图.

I currently have a Plotly heatmap with categorical axes.

热图绘制图上的个体对自己的图,每对个体具有分配给该对的整数值(z值,即在热图上绘制的值)(这是对如何进行度量的度量)分开我们的一对).目前,热图上的颜色已自动分配.

The heat map plots the individuals of a graph against themselves, with each pair of individuals having a integer value (the z-value, which is what is plotted on the heat map) assigned to that pair (it's a measure of how fair apart the pair our). Right now, the colors on the heat map are being automatically assigned.

不幸的是,我不太清楚如何为这种热图指定颜色方案.是否可以指定用于每个z值的颜色?例如,我可以设置1 =蓝色,2 =红色,3 =绿色等吗?

Unfortunately, I can't quite figure out how to specify the color scheme for such a heat map. Is it possible to specify the colors that are used for each z-value? For example, could I set 1 = blue, 2 = red, 3 = green, etc. ?

推荐答案

我遇到了完全相同的问题,并设法通过一些小技巧解决了这个问题.这个问题似乎与发布答案最相关.到目前为止,Plotly不允许您明确地执行此操作,即为分类(离散)热图值指定确切的颜色.此问题记录在 github问题中.一个纯JS解决方案链接到那里,我花了一些时间在R中重现(使用Plotly 4.7.1).

I had exactly the same problem and I managed to figure it out with some small hacks. This question seem the most relevant to post the answer. As for now, Plotly won't let you do that explicitly, that is to specify exact colors to categorical (discrete) heatmap values. This is documented in this github issue. A pure JS solution is linked there and it took me a while to reproduce it in R (using Plotly 4.7.1).

首先,代替一个包含所有数据的矩阵,每个类别需要一个矩阵,每个矩阵都填充一个和NA.每个矩阵的大小必须相同.让我们来整理一些数据.

First, instead of one matrix holding all the data, you need to have one matrix per category, each filled with ones and NA's. Each matrix must be the same size. Let's make up some data.

greenSeriesData <- matrix(c(1,1,NA,NA,1,NA,NA,NA,1), nrow = 3)
redSeriesData <- matrix(c(NA,NA,1,1,NA,NA,NA,NA,NA), nrow = 3)
blueSeriesData <- matrix(c(NA,NA,NA,NA,NA,1,1,1,NA), nrow = 3)

然后,对于每个类别,您需要有一个单独的data.frame,其中包含给定类别的颜色.重要的是这些data.frames必须没有名称.

Then for each category you need to have a separate data.frame holding color for given category. What's important is that those data.frames needs to have no colnames.

greenColor <- data.frame(x = c(0,1), y = c("#63a803", "#63a803"))
colnames(greenColor) <- NULL

redColor <- data.frame(x = c(0,1), y = c("#a80b03", "#a80b03"))
colnames(redColor) <- NULL

blueColor <- data.frame(x = c(0,1), y = c("#035da8", "#035da8"))
colnames(blueColor) <- NULL

您已经准备好将每个类别指定为单独的轨迹:

And you're ready to plot specifying each category as a separate trace:

plot_ly(
    type = "heatmap"
) %>% add_trace(
    z = greenSeriesData,
    colorscale = greenColor
) %>% add_trace(
    z = redSeriesData,
    colorscale = redColor
) %>% add_trace(
    z = blueSeriesData,
    colorscale = blueColor
)

如果要进一步调整图例以使其看起来更好,则需要在每个跟踪中添加一个colorbar参数:

If you want to further adjust the legend to look good, you would need to add a colorbar argument to each trace:

plot_ly(
    type = "heatmap"
) %>% add_trace(
    z = greenSeriesData,
    colorscale = greenColor,
    colorbar = list(
        len = 0.3,
        y = 1,
        yanchor = 'top',
        title = 'Green series',
        tickvals = ''
    )
) %>% add_trace(
    z = redSeriesData,
    colorscale = redColor,
    colorbar = list(
        len = 0.3,
        y = 1,
        yanchor = 'top',
        title = 'Red series',
        tickvals = ''
    )
) %>% add_trace(
    z = blueSeriesData,
    colorscale = blueColor,
    colorbar = list(
        len = 0.3,
        y = 1,
        yanchor = 'top',
        title = 'Blue series',
        tickvals = ''
    )
)

这篇关于在绘图热图中指定颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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