Search code examples
rplotlyr-plotly

Delete text from bars of plotly chart


In the plotly bar chart below how can I keep only the hoverinfo and skip the text inside the bars?

library(plotly)

x <- c('Product A', 'Product B', 'Product C')
y <- c(20, 14, 23)
data <- data.frame(x, y)

fig <- plot_ly(
  data = as.data.frame(data),
  x = ~ x,
  y = ~ y,
  type = "bar",
  marker = list(color = 'rgb(158,202,225)',
                line = list(color = 'rgb(8,48,107)',
                            width = 1.5))
  ,
  text = paste(
    "X :",
    data$x,
    "<br> Count of Issue :",
    data$y
  ),
  hoverinfo = "text"
) 

fig

Solution

  • From the very long layout documentation about text:

    If trace hoverinfo contains a "text" flag and "hovertext" is not set, these elements will be seen in the hover labels.

    So, we just need to pass in the parameter as hovertext instead of text.

    fig <- plot_ly(
      data = as.data.frame(data),
      x = ~ x,
      y = ~ y,
      type = "bar",
      marker = list(color = 'rgb(158,202,225)',
                    line = list(color = 'rgb(8,48,107)',
                                width = 1.5))
      ,
      hovertext = paste(
        "X :",
        data$x,
        "<br> Count of Issue :",
        data$y
      ),
      hoverinfo = "text"
    ) 
    
    fig
    

    enter image description here