简体   繁体   English

R plotly中的离散颜色条

[英]Discrete colorbar in R plotly

I am trying to produce a Heatmap with R plotly.我正在尝试用 R 绘制热图。 This is the reproducible example:这是可重现的示例:

test <- structure(list(s1 = c(0L, 0L, 1L, 0L, 1L, 1L), s2 = c(1L, 1L, 
0L, 1L, 0L, 0L), s3 = c(0L, 0L, 0L, 0L, 0L, 0L), s4 = c(0L, 0L, 
0L, 0L, 0L, 0L), s5 = c(0L, 0L, 0L, 0L, 0L, 0L), s6 = c(0L, 0L, 
0L, 0L, 0L, 0L)), .Names = c("s1", "s2", "s3", "s4", "s5", "s6"
), row.names = c("5HT2 type receptor mediated signaling pathway", 
"5HT3 type receptor mediated signaling pathway", "5-Hydroxytryptamine degredation", 
"Alpha adrenergic receptor signaling pathway", "Alzheimer disease-amyloid secretase pathway", 
"Angiogenesis"), class = "data.frame")

And the code to produce the heatmap in plotly:以及生成热图的代码:

f1 <- list(
    family = "Arial, sans-serif",
    size = 5,
    color = "lightgrey")

f2 <- list(
    family = "Old Standard TT, serif",
    size = 10,
    color = "black")

a <- list(
  title = "",
  titlefont = f1,
  showticklabels = TRUE,
  tickangle = 45,
  tickfont = f2,
  exponentformat = "E")

plot_ly(z = as.matrix(test), 
        zmin=0, 
        zmax=1,
        x = colnames(test), 
        xgap = 2, 
        y = rownames(test), 
        ygap =2, 
        type = "heatmap", 
        colorbar=list(ypad = 30, tickmode="array", tickvals = c(0,1), color = 2, autocolorscale = F )) %>% 
  layout(xaxis = a,
         margin = list(l =500, r = 10, b = 200, t = 10))

As you see, the produced plot has a scale that is continuous.如您所见,生成的图具有连续的比例。 I have found this StackOverflow question where the author asked a similar question.我发现了这个StackOverflow question ,作者问了一个类似的问题。 I tried to replicate the solutions provided by the answers but I couldn't get to the bottom of it.我试图复制答案提供的解决方案,但我无法深入了解。 Could you please tell me how to set the scale to discrete instead of continuous color ?你能告诉我如何将比例设置为离散而不是连续颜色吗?

Thanks谢谢

EDIT1: Added "a" variable definition EDIT1:添加了“a”变量定义

You need to define a color scale as follows:您需要按如下方式定义色标:

colorScale <- data.frame(z=c(0,0.5,0.5,1),col=c("#440053","#440053","#FDE624","#FDE624"))
colorScale$col <- as.character(colorScale$col)

plot_ly(z = as.matrix(test), 
        x = colnames(test), 
        xgap = 2, 
        y = rownames(test), 
        ygap =2, 
        type = "heatmap", 
        colorscale=colorScale ,
        colorbar=list(ypad = 30, tickvals=c(0.25,0.75), ticktext=c(0,1))) %>% 
  layout(xaxis = a,
         margin = list(l =500, r = 10, b = 200, t = 10))

在此处输入图片说明

Macro Sandris answer is perfect and works like a charm. Macro Sandris 的回答是完美的,并且很有魅力。

I just want to add, that if you need more than two classes, you may do so by using the following example:我只想补充一点,如果您需要两个以上的类,您可以使用以下示例:

2 classes (see Marcos answer) 2节课(见马科斯回答)

colorScale <- data.frame(z=c(0,0.5,0.5,1),col=c("#440053","#440053","#FDE624","#FDE624"))

3 classes 3班

colorScale <- data.frame(z=c(0,0.33,0.33,0.66,0.66,1),col=c("#440053","#440053","#FDE624","#FDE624","#DDE431","#DDE431"))

and so on.等等。 Make sure the vector passed into the data frame always ranges from 0 to 1.确保传入数据框的向量始终介于 0 到 1 之间。

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

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