tidyverse / ggplot2

An implementation of the Grammar of Graphics in R
https://ggplot2.tidyverse.org
Other
6.39k stars 2k forks source link

ggplotly stopped working with version 3.5.1 #5892

Closed raphaludwig closed 1 month ago

raphaludwig commented 1 month ago

I have encountered an issue with the current version of ggplot2 (3.5.1) when integrating with the plotly package via the ggplotly function. The provided code should generate a Plotly plot without any errors.

library(plotly)
library(ggplot2)

dat <- data.frame(
  time = factor(c("Lunch", "Dinner"), levels = c("Lunch", "Dinner")),
  total_bill = c(14.89, 17.23)
)

p <- ggplot(data = dat, aes(x = time, y = total_bill)) +
  geom_bar(stat = "identity")

fig <- ggplotly(p)

fig

p2 <- ggplot(data = dat, aes(x = time, y = total_bill, fill = time)) +
  geom_bar(stat = "identity")

fig2 <- ggplotly(p2)

fig2

When running the code without an aes() component that generates legend elements (such as color or fill), the plot is created without issues. However, including an aesthetic component that generates a legend (e.g., fill) results in the following error:

Error in train(..., self = self) : unused argument (list("time", "total_bill", "time"))

This issue appears to be specific to the interaction between ggplot2 and plotly, particularly when using aesthetics that produce legends. In previous versions of ggplot2 this error did not occur. Any insights or resolutions for this problem would be highly appreciated

cgostic commented 1 month ago

Can confirm this issue. Here's a reproducible example:

library(ggplot2)
library(plotly)
df <- data.frame(a = c(rep(1:25, 3)),
                 b = runif(75, 0, 100),
                 c = c(rep('c1', 25),
                       rep('c2', 25),
                       rep('c3', 25)))

# Without legend
p1 <- ggplot(df) +
  geom_line(aes(a,b)) +
  labs(x = 'Var A', y = 'Var b')

# Adding "color" aesthetic, which defaults to a legend
p2 <- ggplot(df) +
  geom_line(aes(a, b, color = c)) +
  labs(x = 'Var A', y = 'Var b')

# Works fine
ggplotly(p1)

# Yields error
ggplotly(p2)

p2 above (which includes a legend) yields the error below, which calls out an issue with the "labels" item of the ggplot object as an unused argument.

"Error in train(..., self = self) : 
  unused argument (list("Var A", "Var b", "c"))"

sessionInfo:

R version 4.2.2 (2022-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows Server x64 (build 17763)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] plotly_4.10.3 ggplot2_3.5.1

This error does not occur with ggplot2-3.4.4

yutannihilation commented 1 month ago

plotly_4.10.3

I confirmed the error is reproducible with this a bit outdated version. Could you try updating it to 4.10.4, the latest CRAN version?

teunbrand commented 1 month ago

Can confirm that this works as intended with plotly 4.10.4. As there isn't anything to fix on either ggplot2's side or plotly's side, I'm going ahead and close this issue.