Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
543 views
in Technique[技术] by (71.8m points)

dplyr - R: Adding a "tool tip" to interactive plot (plotly)

I am using the R programming language. From a previous post (R: Plot not Fully Loading), I learned how to make interactive plots in R using plotly :

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

Suppose if I was to add an "id" column to the data set, e.g.

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

Is it possible so that when you "click" on any of the "lines" on this plot, information from the dataset (i.e. "df") corresponding to row of that line appears?

enter image description here

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Did not run your code, but you may need to prepare all the text as one string in the column beforehand and use text = ~tooltip_column to display it. It may work with for line splits

  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
  )

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...