自定义绘图热图的悬停文本

dan*_*dan 5 r hover heatmap plotly

我有一个matrix(来自几个条件的基因表达):

set.seed(1)
mat <- matrix(rnorm(50*10),nrow=50,ncol=10,dimnames=list(paste("C",1:50,sep="."),paste("G",1:10,sep=".")))
Run Code Online (Sandbox Code Playgroud)

我想将其绘制为heatmap使用plotlyin R

require(plotly)
heatmap.plotly <- plot_ly(x=colnames(mat),y=rownames(mat),z=mat,type="heatmap",colors=colorRamp(c("darkblue","white","darkred")),colorbar=list(title="Score",len=0.4)) %>%
  layout(yaxis=list(title="Condition"),xaxis=list(title="Gene"))
Run Code Online (Sandbox Code Playgroud)

工作正常。

但是,我想添加仅在悬停时才能看到的文本。

我认为这会奏效:

conditions.text <- paste(paste("C",1:50,sep="."),rep(paste(LETTERS[sample(26,10,replace=T)],collapse=""),50),sep=":")
heatmap.plotly <- plot_ly(x=colnames(mat),y=rownames(mat),z=mat,type="heatmap",colors=colorRamp(c("darkblue","white","darkred")),colorbar=list(title="Score",len=0.4),hoverinfo='text',text=~conditions.text) %>%
  layout(yaxis=list(title="Condition"),xaxis=list(title="Gene"))
Run Code Online (Sandbox Code Playgroud)

但事实并非如此。将鼠标悬停在情节上时,我实际上看不到任何文字。

请注意,我正在使用 amatrix而不是melted data.frame.

Max*_*ers 5

您将一个 50x10 的数组传递到热图中,但将 50 个条目的列表作为悬停信息传递。热图和文本的输入必须具有相同的尺寸。

library(plotly)
set.seed(1)
mat <- matrix(rnorm(50*10),nrow=50,ncol=10,dimnames=list(paste("C",1:50,sep="."),paste("G",1:10,sep=".")))

conditions.text <- paste(paste("C",1:50,sep="."),rep(paste(LETTERS[sample(26,10,replace=T)],collapse=""),500),sep=":")
conditions.text <- matrix(unlist(conditions.text), ncol = 10, byrow = TRUE)

plot_ly(z=mat,
        type="heatmap",
        hoverinfo='text',
        text=conditions.text)
Run Code Online (Sandbox Code Playgroud)