在 R 绘图中选择/复制悬停文本

问题描述 投票:0回答:1

我希望能够选择并复制图表中的悬停文本。 R API 可以做到这一点吗? 例如,在此热图中,将鼠标悬停在坐标(X = a,Y = d)上时,可以看到悬停文本显示:

x=a,

y=d,

z=0.71886

但是,文本不可选择。目标是例如在一个框中显示此文本(可能通过右键单击相应的单元格)以便能够将文本内容复制到剪贴板中。 任何帮助将不胜感激。

r hover plotly
1个回答
0
投票

我已经能够使用以下代码提取值:

library(plotly)
m <- matrix(rnorm(9), nrow = 3, ncol = 3)
fig <- plot_ly(x = c("a", "b", "c"),
               y = c("d", "e", "f"),
               z = m, type = "heatmap")

x <- fig$x$attrs[[1]][[1]]
y <- fig$x$attrs[[1]][[2]]
z <- fig$x$attrs[[1]][[3]]

mat <- matrix(nrow = 9, ncol = 3)
counter <- 1

for(i in 1 : 3)
{
  for(j in 1 : 3)
  {
    mat[counter, ] <- c(x[i], y[j], z[j, i])
    counter <- counter + 1
  }
}

mat
     [,1] [,2] [,3]                 
 [1,] "a"  "d"  "-1.24626999077972"  
 [2,] "a"  "e"  "-0.0513546366077807"
 [3,] "a"  "f"  "0.131577832947162"  
 [4,] "b"  "d"  "0.36052924344838"   
 [5,] "b"  "e"  "-2.01674679871447"  
 [6,] "b"  "f"  "-0.0783806771351291"
 [7,] "c"  "d"  "-1.3994093901524"   
 [8,] "c"  "e"  "-0.443892523865165" 
 [9,] "c"  "f"  "0.620093087480776"  
© www.soinside.com 2019 - 2024. All rights reserved.