[英]Customize hover text for plotly heatmap
我有一个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=".")))
我希望在R
使用plotly
绘制为heatmap
。
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"))
工作良好。
但是,我想添加仅在悬停时才会看到的文本。
我认为这会奏效:
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"))
但事实并非如此。 实际上,当我将鼠标悬停在情节上时,我看不到任何文字。
请注意,我正在使用matrix
而不是melted
data.frame
。
您将50x10的数组传递到热图中,但是将50个条目列为hoverinfo。 热图和文本的输入必须具有相同的尺寸。
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)
因此,plotly中的~
语法被设计为作为data = ...
对象的引用,就像data$...
。 由于plotly的热图不适用于data
参数,因此在这里不起作用。 您将需要构造一个与mat
相同的矩阵,以提供给text = ...
参数。 有点笨重,但它有很好的情节:
# make a matrix same dimensions as mat
text.mat <- matrix(conditions.text, nrow(mat), ncol(mat))
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=text.mat) %>%
layout(yaxis=list(title="Condition"),xaxis=list(title="Gene"))
heatmap.plotly
如果要为文本构建多行hoverinfo,只需在text.mat
使用内联<\\br>
标记, text.mat
其作为html读取,并在呈现时创建行返回。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.