0
votes

I create a basic scatterplot with plotly like the one below. The issue is that while I set specifically the text inside the hoverinfo the numeric values are displayed one more time- (20,56) -before the actual text- Team Pts:20 Fantasy Pts: 56 -that I wish to display. How can I delete them?

pts<-c(10,20,30)
npts<-c(24,56,78)
ex<-data.frame(pts,npts)


library(plotly)
p <- plot_ly(data = ex, x = ~pts, y = ~npts,
             marker = list(size = 10,
                           color = 'white',
                           line = list(color = 'rgba(152, 0, 0, .8)',
                                       width = 2))) %>%
  add_trace(
    text = ~paste("Team Pts: ", pts, '</br>Fantasy Pts:', npts),
    hoverInfo='text'
  )
p
2

2 Answers

1
votes

One way of doing this is to add the text to each data point, by adding in a variable to the hovertemplate parameter.

I don't have a way to test this at the moment, but it should look something like this:

add_trace(
           x = ~pts,
           y = ~npts,
           hovertemplate = paste('<i>Team points</i>: %{x}',
                                '<br><b>Fantasy Pts</b>: %{y}</br>',
                                 )
      )
1
votes

You just misspelled the argument hoverInfo, which should be hoverinfo, so your plot used the default hoverinfo = "all". Also, replace </br> by <br> to display the hover text on two lines:

library(plotly)

ex <- data.frame(
    pts = c(10, 20, 30),
    npts = c(24, 56, 78)
)

plot_ly(data = ex, 
    type = "scatter",
    mode = "markers",
    x = ~pts, 
    y = ~npts, 
    text = ~paste("Team Pts: ", pts, '<br>Fantasy Pts:', npts), 
    hoverinfo = "text"
)

enter image description here