[英]Plotly: how to add text from another dataframe to a matrix heatmap in R?
After reading the following post , I tried to reproduce the example.阅读以下帖子后,我尝试重现该示例。 However, the problem is that they have a simple matrix where one column is X and the other is Y. In my case, I have multiple columns that I am trying to plot.
但是,问题是它们有一个简单的矩阵,其中一列是 X,另一列是 Y。在我的情况下,我有多个要绘制的列。 See example below.
请参阅下面的示例。
This is a reproducible example data:这是一个可重现的示例数据:
testdata <- structure(list(P10 = c(1.3210675542847, 2.36327446015004, 2.57222510645425,
1.51129279647688), P21 = c(0, 0, 0, 2.54308916286001), P12 = c(1.7753478270376,
0, 1.5690304967756, 1.97404318110563), P18 = c(0, 0, 0, 2.72391520754351
), P5 = c(1.73384124079386, 2.94055776224166, 3.02224210965803,
0)), row.names = c("Ras ", "p53 feedback loops 2", "Apoptosis ",
"Metabotropic glutamate receptor group III "), class = "data.frame")
the text I would like to have in the heatmap:我想在热图中的文字:
structure(list(P10 = c("0", "0", "0", "0"), P21 = c("11/69",
"11/47", "20/107", "11/64"), P12 = c("0", "0", "0", "0"), P18 = c("10/69",
"0", "13/107", "10/64"), P5 = c("0", "0", "0", "0")), row.names = c("Ras ",
"p53 feedback loops 2", "Apoptosis ", "Metabotropic glutamate receptor group III "
), class = "data.frame")
and the following plot.ly code:以及以下 plot.ly 代码:
library(plotly)
f1 <- list(
family = "Arial, sans-serif",
size = 5,
color = "lightgrey"
)
f2 <- list(
family = "Old Standard TT, serif",
size = 12,
color = "black"
)
a <- list(
title = "",
titlefont = f1,
showticklabels = TRUE,
tickangle = 45,
tickfont = f2,
exponentformat = "E"
)
colorScale <- data.frame(z=c(0,0.1,0.1,0.2,0.2,0.3,0.3,0.4,0.4,0.5,0.5,0.6,0.6,0.7,0.7,0.8,0.8,0.9,0.9,1),col=c("#BEBEBE", "#0000FF","#0000FF", "#1C00E2","#1C00E2", "#3800C6","#3800C6", "#5500AA","#5500AA", "#71008D","#71008D", "#8D0071","#8D0071", "#AA0055","#AA0055", "#C60038","#C60038", "#E2001C","#E2001C", "#FF0000"))
colorScale$col <- as.character(colorScale$col)
plot_ly(z = as.matrix(testdata),
zmin=0,
zmax=6,
x = colnames(testdata),
xgap = 0.6,
y = rownames(testdata),
ygap =0.6,
colorscale=colorScale ,
type = "heatmap",
colorbar=list(ypad = 50, tickvals=seq(6,0, -3), ticktext=c("6", "3", "0", "NA") )) %>%
layout(xaxis = a,
margin = list(l = 400,
r = 10,
b = 100,
t = 10)) %>%
add_annotations(text = as.character(text),
showarrow = F,
font=list(color='black'))
produces the following heatmap:产生以下热图:
I tried to use different approaches like providing the whole dataframe text = text,
or text = text[,1],
but nothing was getting plotted...我尝试使用不同的方法,例如提供整个数据框
text = text,
或text = text[,1],
但没有绘制任何内容...
What is going wrong ?出了什么问题? How can I display the text correctly when I have another dataframe of text I want to add to the heatmap ?
当我有另一个要添加到热图中的文本数据框时,如何正确显示文本?
Edit 1 : trying psychOle answer编辑 1 :尝试 psychOle 答案
I tried the proposed answer by @psychOle but it isn't working, the text does display but not as it should be like in text
.我试图通过@psychOle所提出的答案,但它不工作,文本不显示,但不能因为它应该像
text
。 Check it below:在下面检查:
Use t(text)
:使用
t(text)
:
plot_ly(z = as.matrix(testdata),
zmin=0,
zmax=6,
x = colnames(testdata),
xgap = 0.6,
y = rownames(testdata),
ygap =0.6,
colorscale=colorScale ,
type = "heatmap",
colorbar=list(ypad = 50, tickvals=seq(6,0, -3), ticktext=c("6", "3", "0", "NA") )) %>%
layout(xaxis = a,
margin = list(l = 00,
r = 10,
b = 100,
t = 10)) %>%
add_annotations(text=t(text),
showarrow = F,
font=list(color='black'))
Please consider providing a minimal working example for your next question.请考虑为您的下一个问题提供一个最小的工作示例。
this works for me这对我有用
cor_dat = melt(cor_dat)
plot_ly(z = as.matrix(testdata),
zmin=0,
zmax=6,
x = colnames(testdata),
xgap = 0.6,
y = rownames(testdata),
ygap =0.6,
colorscale=colorScale ,
type = "heatmap",
colorbar=list(ypad = 50, tickvals=seq(6,0, -3), ticktext=c("6", "3", "0", "NA") )) %>%
layout(xaxis = a,
margin = list(l = 00,
r = 10,
b = 100,
t = 10)) %>%
add_annotations(x = cor_dat$Var1,
y = cor_dat$Var2,
text = cor_dat$value,
showarrow = F,
font=list(color='black'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.