stats_noob
stats_noob

Reputation: 5907

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

Upvotes: 2

Views: 1331

Answers (1)

glatocha
glatocha

Reputation: 131

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 \n 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
  )

Upvotes: 1

Related Questions