用多种颜色为轴刻度文本着色

Coloring the axis tick text by multiple colors

我正在尝试使用 Rplotly 包绘制 heatmap,我希望在其中为 [=40= 的特定标签设置特定颜色]勾选文字。

这是一个示例数据集:

set.seed(1)
df <- reshape2::melt(matrix(rnorm(100),10,10,dimnames = list(paste0("G",1:10),paste0("S",1:10))))

这就是我正在尝试的:

library(plotly)
library(dplyr)
plot_ly(z=c(df$value),x=df$Var2,y=df$Var1,colors=grDevices::colorRamp(c("darkblue","gray","darkred")),type="heatmap",colorbar=list(title="Scaled Value",len=0.4)) %>%
  layout(yaxis=list(title=list(color=c(rep("darkred",5),rep("darkblue",5)))))

它不起作用,因为我得到:

变化:yaxis=list(title=list(color=c(rep("darkred",5),rep("darkblue",5))))

收件人:yaxis=list(title=list(color=list(c(rep("darkred",5),rep("darkblue",5)))))

或者:yaxis=list(title=list(tickcolor=c(rep("darkred",5),rep("darkblue",5))))

或:yaxis=list(title=list(tickcolor=list(c(rep("darkred",5),rep("darkblue",5)))))

似乎没有帮助。

有什么想法吗?

Python 已在此处提出并回答了具有不同设置的类似问题:。以下是我对 R 设置的最佳尝试。

剧情:

代码:

library(plotly)
library(dplyr)

# data
set.seed(1)
df <- reshape2::melt(matrix(rnorm(100),10,10,dimnames = list(paste0("G",1:10),paste0("S",1:10))))

# plotly setup
p1 <- plot_ly(z=c(df$value),x=df$Var2,y=df$Var1,colors=grDevices::colorRamp(c("darkblue","gray","darkred")),
              type="heatmap",colorbar=list(title="Scaled Value",len=0.4)) 

p2 <- p1 %>% add_trace(xaxis='x2', showscale=FALSE)

p3 <- p2  %>% layout(xaxis=list(range=list(0,9),
                                tickvals=list(0,1,2,3,4),
                                tickfont=list(color='red')),
                     xaxis2=list(range=list(0,9), overlaying='x',
                                 ticktext = list('s9', 's10'),
                                 tickvals=list(5, 6, 7, 8, 9),
                                 tickfont=list(color='blue')))
# plot it
p3