Plotly:如何将文本从另一个数据帧添加到 R 中的矩阵热图中?
Plotly: how to add text from another dataframe to a matrix heatmap in R?
阅读以下post后,我尝试重现该示例。但是,问题是它们有一个简单的矩阵,其中一列是 X,另一列是 Y。在我的例子中,我有多个列要绘制。请参阅下面的示例。
这是一个可重现的示例数据:
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")
我希望在热图中包含的文本:
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")
和以下 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'))
生成以下热图:
我尝试使用不同的方法,例如提供整个数据框 text = text,
或 text = text[,1],
但没有绘制任何内容...
出了什么问题?当我有另一个要添加到热图的文本数据框时,如何正确显示文本?
编辑 1:尝试 psychOle 答案
我尝试了@psychOle 提出的答案,但它不起作用,文本确实显示,但不像 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'))
请考虑为您的下一个问题提供 minimal working example。
这对我有用
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'))
阅读以下post后,我尝试重现该示例。但是,问题是它们有一个简单的矩阵,其中一列是 X,另一列是 Y。在我的例子中,我有多个列要绘制。请参阅下面的示例。
这是一个可重现的示例数据:
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")
我希望在热图中包含的文本:
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")
和以下 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'))
生成以下热图:
我尝试使用不同的方法,例如提供整个数据框 text = text,
或 text = text[,1],
但没有绘制任何内容...
出了什么问题?当我有另一个要添加到热图的文本数据框时,如何正确显示文本?
编辑 1:尝试 psychOle 答案
我尝试了@psychOle 提出的答案,但它不起作用,文本确实显示,但不像 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'))
请考虑为您的下一个问题提供 minimal working example。
这对我有用
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'))