Skip to content

ggplot2::stat_summary with ggplot2::geom_crossbar inducing error in ggplotly #2220

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

Closed
akunihiro opened this issue Jan 3, 2023 · 1 comment · Fixed by #2222
Closed

ggplot2::stat_summary with ggplot2::geom_crossbar inducing error in ggplotly #2220

akunihiro opened this issue Jan 3, 2023 · 1 comment · Fixed by #2222

Comments

@akunihiro
Copy link


I am running into an issue after recently updating plotly (and possibly ggplot2). Using ggplot2::geom_crossbar in ggplot2::stat_summary and visualizing with ggplotly results in the following error:

Error in `[[<-.data.frame`(`*tmp*`, nm, value = numeric(0)) : 
  replacement has 0 rows, data has 3

Here is a reprex:

library(ggplotly)
library(plotly)
library(tidyverse)
library(ggbeeswarm)

data("iris")

iris_plot <- iris %>%
  ggplot(aes(x = Species, y = Sepal.Length)) +
  
  # plot quasirandom distribution
  geom_quasirandom(shape = 21,
                   # size = 2,
                   size = 4,
                   alpha = 1,
                   stroke = 0.1,
                   varwidth = TRUE) +
  
  # add median bar
  stat_summary(fun = function(x) log2(median(2^x)),
               geom = "crossbar",
               linetype = "dashed",
               color = "black") +
  
  # add mean bar
  stat_summary(fun = function(x) log2(mean(2^x)),
               geom = "crossbar",
               linetype = "solid",
               color = "black") +
  
  # log2 scale
  scale_y_continuous(trans = "log2")

# plotting directly works
iris_plot

# plot with ggplotly results in error
ggplotly(iris_plot) 

Changing the geom argument in ggplot2::stat_summary to "pointrange" allows the plot to be visualized with ggplotly

cpsievert added a commit that referenced this issue Jan 3, 2023
@cpsievert
Copy link
Collaborator

Here's a more minimal reprex:

library(plotly)

p <- ggplot(iris, aes(x = Species, y = Sepal.Length)) +
    stat_summary(fun = median, geom = "crossbar")

ggplotly(p) 

cpsievert added a commit that referenced this issue Jan 4, 2023
…ssbar()` (#2222)

* Close #2220: provide a default if the fatten param is missing in to_basic.GeomCrossbar

* Add snapshot

* Update NEWS; wait before taking 2nd screenshot
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

Successfully merging a pull request may close this issue.

2 participants