R中的离散颜色条 [英] Discrete colorbar in R plotly

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

问题描述

我正在尝试用 R 绘制热图.这是可重现的示例:

test <- 结构(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型受体介导的信号通路",5HT3型受体介导的信号通路",5-羟色胺降解",α肾上腺素受体信号通路",阿尔茨海默病-淀粉样蛋白分泌酶通路","血管生成"), 类 = "data.frame")

以及在 plotly 中生成热图的代码:

f1 <- list(family = "Arial, sans-serif",大小 = 5,颜色=浅灰色")f2 <- 列表(family = "旧标准 TT,衬线",大小 = 10,颜色=黑色")一个 <- 列表(标题 = "",标题字体 = f1,showticklabels = TRUE,刻度角 = 45,滴答字体 = f2,指数格式 = "E")plot_ly(z = as.matrix(test),zmin=0,zmax=1,x = colnames(测试),xgap = 2,y = 行名(测试),ygap = 2,类型=热图",colorbar=list(ypad = 30, tickmode="array", tickvals = c(0,1), color = 2, autocolorscale = F)) %>%布局(x轴=一个,边距 = 列表(l = 500,r = 10,b = 200,t = 10))

如您所见,生成的绘图具有连续的比例.我发现这个

I am trying to produce a Heatmap with R plotly. 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. 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

解决方案

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

这篇关于R中的离散颜色条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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