Skip 'trace0' from hovertemplate of plotly in R

Advertisements

I have the plotly plot below and I want to skip this "trace0" value next to the hovertemplate I have created.

library(plotly)

fig <- plot_ly() 
fig <- fig %>%
  add_trace(
    type = 'scatter',
    mode = 'lines+markers',
    x = c(1,2,3,4,5),
    y = c(2.02825,1.63728,6.83839,4.8485,4.73463),
    text = c("Text A", "Text B", "Text C", "Text D", "Text E"),
    hovertemplate = paste('<i>Price</i>: $%{y:.2f}',
                        '<br><b>X</b>: %{x}<br>',
                        '<b>%{text}</b>'),
    showlegend = FALSE
  ) 

>Solution :

You should add the <extra></extra> tags to remove the extra box which is your trace0 tag like described here in the docs under hovertemplate. You could use the following code:

library(plotly)

fig <- plot_ly() 
fig <- fig %>%
  add_trace(
    type = 'scatter',
    mode = 'lines+markers',
    x = c(1,2,3,4,5),
    y = c(2.02825,1.63728,6.83839,4.8485,4.73463),
    text = c("Text A", "Text B", "Text C", "Text D", "Text E"),
    hovertemplate = paste('<i>Price</i>: $%{y:.2f}',
                          '<br><b>X</b>: %{x}<br>',
                          '<b>%{text}</b>',
                          '<extra></extra>'),
    showlegend = FALSE
  ) 
fig

Created on 2023-03-07 with reprex v2.0.2


Example without the trace0:

Leave a Reply Cancel reply