Search code examples
rggplot2plotly

Error in -data$group : invalid argument to unary operator


I would like to use frame with ggplotly to have an animated histogram. When I run the following code it returns an error:

# Reproducible data
set.seed(7)
df = data.frame(
  day = factor(rep(1:7, each = 100)),
  value = runif(700, 0, 1)
)
library(ggplot2)
library(plotly)

p <- ggplot(df, aes(x = value, frame = day)) +
  geom_histogram()
ggplotly(p)
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
#> Error in -data$group: invalid argument to unary operator

Created on 2023-04-19 with reprex v2.0.2

I found this issue, but this is with regard to using gganimate with geom_bar. So I was wondering if anyone knows how to fix this error to create an animated histogram with ggplotly?


Solution

  • I found a workaround by using position = "identity" which will create an animated histogram in ggplotly like this:

    library(ggplot2)
    library(plotly)
    
    p <- ggplot(df, aes(x = value, frame = day)) +
      geom_histogram(position = "identity")
    ggplotly(p)
    

    Created on 2023-04-19 with reprex v2.0.2