剧情:如何将文本从另一个数据框中添加到R中的矩阵热图?

时间:2019-02-21 07:15:52

标签: r plotly heatmap r-plotly

阅读以下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'))

产生以下热图:

heatmap plot

我尝试使用不同的方法,例如提供整个数据帧text = text,text = text[,1],,但是没有绘制任何内容...

出了什么问题?当我有另一个要添加到热图的文本数据框时,如何正确显示文本?

编辑1 :尝试psychOle答案

我尝试了@psychOle提出的答案,但是它没有用,虽然确实显示了文本,但是却没有显示text中的样子。在下面检查:

psychOle plot

2 个答案:

答案 0 :(得分:1)

使用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

答案 1 :(得分:1)

这对我有用

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'))