Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ggplotly stopped working with version 3.5.1 #5892

Closed
raphaludwig opened this issue May 15, 2024 · 3 comments
Closed

ggplotly stopped working with version 3.5.1 #5892

raphaludwig opened this issue May 15, 2024 · 3 comments

Comments

@raphaludwig
Copy link

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
Copy link

cgostic commented May 17, 2024

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
Copy link
Member

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
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants