0

Can we make these values seen only when hovered . Example "Toyota Corolla" to be seen only when hovered. Now all the values are appearing

library(plotly)

data <- mtcars[which(mtcars$am == 1 & mtcars$gear == 4),]

fig <- plot_ly(data, x = ~wt, y = ~mpg, type = 'scatter', mode = 'markers',
        marker = list(size = 10))
fig <- fig %>% add_annotations(x = data$wt,
                  y = data$mpg,
                  text = rownames(data),
                  xref = "x",
                  yref = "y",
                  showarrow = TRUE,
                  arrowhead = 4,
                  arrowsize = .5,
                  ax = 20,
                  ay = -40,
                  # Styling annotations' text:
                  font = list(color = '#264E86',
                              family = 'sans serif',
                              size = 14))

fig
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
manu p
  • 952
  • 4
  • 10
  • Have you seen this? https://stackoverflow.com/questions/38917101/how-do-i-show-the-y-value-on-tooltip-while-hover-in-ggplot2 – broti Nov 11 '21 at 19:24
  • Looks like this is ggplots. Not sure if this can be replicated – manu p Nov 11 '21 at 19:26

1 Answers1

0

To add the row names/car types so that they appear when you hover over the point, you just need to add text and use it in hoverinfo

In your example, you can use rownames() to get the car types and add them to text which then can be used by hoverinfo

fig <- plot_ly(data, x = ~wt, y = ~mpg, type = 'scatter', mode = 'markers',
               marker = list(size = 10), text = rownames(data), hoverinfo = "text")

However, if you didn't have the car names as row names and just as a column in a dataframe, you can just apply them directly to text which then can be used by hoverinfo

#Not required. Just used to create a column with car names#
data$cars <- rownames(data)

You can then use the new column as text

fig <- plot_ly(data, x = ~wt, y = ~mpg, type = 'scatter', mode = 'markers',
               marker = list(size = 10), text = data$cars, hoverinfo = "text")

Both methods will give you the following result

example1

neuron
  • 1,949
  • 1
  • 15
  • 30