简体   繁体   中英

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. 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:

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...

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

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 . Check it below:

心理情节

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM