R:向交互式绘图添加“工具提示”(绘图)

时间:2021-07-04 07:07:27

标签: r dplyr hover plotly tooltip

我使用的是 R 编程语言。从上一篇文章 (R: Plot not Fully Loading) 中,我学会了如何使用 plotly 在 R 中制作交互式绘图:

library(plotly)

iris %>% plot_ly(type = 'parcoords', line = list(color = ~as.integer(Species), 
         colorscale = list(c(0,'red'),c(0.5,'green'),c(1,'blue'))), 
         dimensions = list( list(range = c(2,4.5), label = 'Sepal Width', values = ~Sepal.Width), 
                      list(range = c(4,8), constraintrange = c(5,6), label = 'Sepal Length', values = ~Sepal.Length), 
                      list(range = c(0,2.5), label = 'Petal Width', values = ~Petal.Width), 
                      list(range = c(1,7), label = 'Petal Length', values = ~Petal.Length) ) )  

enter image description here

假设我要向数据集添加一个“id”列,例如

library(dplyr)
df <- iris %>% mutate(id = row_number())

是否有可能当您“单击”此图上的任何“线”时,会出现与该线的行对应的数据集(即“df”)中的信息?

enter image description here

谢谢

1 个答案:

答案 0 :(得分:0)

没有运行您的代码,但您可能需要事先将所有文本准备为列中的一个字符串,并使用 text = ~tooltip_column 来显示它。 它可以与 \n 一起用于行拆分

  fig <- plot_ly(
    type = 'scatter3D',
    x = -calcDataMeas2$pos_X,
    y = calcDataMeas2$pos_Y,
    z = z_value,
    text = calcDataMeas2$tube_name,
    scene = 'scene1',
    mode = 'markers',
    marker = marker
  )