Search code examples
rplotlyhistogramr-plotly

hoverinfo displaying over bars in plotly histogram R


I'm trying to create an histogram with plotly, but when I define the 'hoverinfo' information, it displays the information over the bars and not 'inside'. enter image description here

diamonds %>% plot_ly(x = ~cut, color = ~clarity, hoverinfo = 'text', text = ~carat)

I tried to use add_histogram() as well, but nothing seems to work. What should I do?


Solution

  • I was intrigued by your comment that you had the most recent plotly version, so I downloaded it and I could replicate your issue. It looks like there has been a change in the way the default text argument is treated in plotly.js, see here:

    textposition for bar traces now defaults to auto so if you want to use text without it appearing on the figure, you’ll need to explicitly set textposition="none"

    Unfortunately, when type="histogram", there is no textposition argument. The way to fix this in your case is to replace the text argument with hovertext:

    library(plotly)
    diamonds %>% 
        plot_ly(
            x = ~cut, 
            color = ~clarity,
            type = "histogram",
            hoverinfo = "text", 
            hovertext = ~carat
        )
    

    This should resolve the issue.