Search code examples
rggplot2histogramggplotlyfacet-grid

Why does hover text not work with ggplotly, geom_histogram and facet_grid?


When using facet_grid() in both rows and columns, with a histogram in ggplotly, the x-axis values turn up as NA in the hover text. This doesn't happen if the faceting is only over rows, only over columns, or if the geom is different e.g. geom_point(). Am I doing something wrong?

MWE:

library(ggplot2)
library(plotly)
data(iris)


ggplotly(
  ggplot(data = iris,
         aes(x = Petal.Length))+
    geom_histogram()+
    facet_grid(Species~round(Sepal.Length))
)

Facet grid histogram with ggplotly


Solution

  • Using ggplot 3.2.1 and plotly 4.9.0, I was able to get this working with a simple workaround—in the ggplotly call, pass the x aesthetic to the tooltip parameter; like so:

    ggplotly(ggplot_histogram, tooltip = c("x", "count"))

    (You also have to pass the count aesthetic; otherwise, kinda defeats the point of a histogram).