Skip to content

ggplotly throws error when called with a ggplot object containing a violin plot that did not have enough data to produce a distribution curve. #1462

Open
@wdm0336

Description

@wdm0336

ggplot is called to produce a violin plot. The data set contains very little data so that the distribution plot is not able to be created. ggplot returns (what I believe to be) a valid ggplot object.

when ggplotly() is called with this object, it throws an error. I would have expected ggplotly() to return without an error as ggplot() did.

library(ggplot2)
library(plotly)

dat <- data.frame(Axis = c("A", "A", "B", "B", "C", "C"),
                  Value = c("0.815724727",
                            "0.727678445",
                            "0.579208876",
                            "0.880850014",
                            "0.999999999",
                            "0.999888945"
                  ))

gg <- ggplot(dat, aes(x = Axis, y = Value)) 

gg <- gg + geom_jitter()

gg <- gg + geom_violin()

# print(gg)
ggplotly(gg)

ggplotly.ggplot() calls gg2list()

Then gg2list() calls layers2traces() at line 216: traces <- layers2traces(data, prestats_data, layout, plot)

layers2traces() is in a loop at line 67. The 2nd list element of data[[i]] is a dataframe with no rows or columns.

What happened is the violin plot did not have enough data for ggplot to create any of the density plots
so the violin plot structure is empty. When I replace the call to ggplotly(gg) with print(gg), violin plots are not produced, only the points from the geom_jitter() call.

ggplotly() is unfortunately, crashing here with the message:

Browse[6]> c
Error in order(data[["y"]], decreasing = TRUE) :
  argument 1 is not a vector
>
Browse[6]> str(data)
List of 2
$ :Classes ‘GeomPoint’ and 'data.frame':	6 obs. of  13 variables:
  ..$ x             : num [1:6] 0.733 0.897 2.355 1.677 3.377 ...
..$ y             : num [1:6] 0.816 0.728 0.579 0.881 1 ...
..$ PANEL         : Factor w/ 1 level "1": 1 1 1 1 1 1
..$ group         : int [1:6] 1 1 2 2 3 3
.. ..- attr(*, "n")= int 3
..$ x_plotlyDomain: chr [1:6] "A" "A" "B" "B" ...
..$ y_plotlyDomain: num [1:6] 0.816 0.728 0.579 0.881 1 ...
..$ shape         : num [1:6] 19 19 19 19 19 19
..$ colour        : chr [1:6] "black" "black" "black" "black" ...
..$ size          : num [1:6] 1.5 1.5 1.5 1.5 1.5 1.5
..$ fill          : logi [1:6] NA NA NA NA NA NA
..$ alpha         : logi [1:6] NA NA NA NA NA NA
..$ stroke        : num [1:6] 0.5 0.5 0.5 0.5 0.5 0.5
$ :Classes ‘GeomViolin’ and 'data.frame':	0 obs. of  0 variables
Browse[6]>
> sessionInfo()
R version 3.5.0 (2018-04-23)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Red Hat Enterprise Linux

Matrix products: default
BLAS: /opt/R/R-3.5.0/lib64/R/lib/libRblas.so
LAPACK: /opt/R/R-3.5.0/lib64/R/lib/libRlapack.so

locale:
  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8
[4] LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8       LC_NAME=C                  LC_ADDRESS=C
[10] LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

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

other attached packages:
  [1] viridis_0.5.1         viridisLite_0.3.0     colourpicker_1.0
[4] tidyr_0.8.1           openxlsx_4.0.17       shinyFiles_0.7.0.9000
[7] DT_0.4                readxl_1.1.0          htmltools_0.3.6
[10] bindrcpp_0.2.2        stringr_1.3.1         markdown_0.8
[13] dplyr_0.7.6           XML_3.98-1.11         visNetwork_2.0.3
[16] shinyTree_0.3.0.9000  usagelogr_0.1.5       httr_1.3.1
[19] plyr_1.8.4            knitr_1.20            plotly_4.8.0.9000
[22] ggplot2_3.0.0.9000    rmarkdown_1.10        shinyjs_1.0
[25] shinyBS_0.61          shiny_1.0.5

loaded via a namespace (and not attached):
  [1] tidyselect_0.2.4  reshape2_1.4.3    purrr_0.2.5       V8_1.5
[5] colorspace_1.3-2  miniUI_0.1.1      yaml_2.1.19       rlang_0.2.2
[9] later_0.7.2       pillar_1.2.2      glue_1.3.0        withr_2.1.2
[13] bindr_0.1.1       cellranger_1.1.0  munsell_0.5.0     gtable_0.2.0
[17] htmlwidgets_1.2   evaluate_0.10.1   labeling_0.3      Cairo_1.5-9
[21] httpuv_1.4.2      crosstalk_1.0.0   curl_3.2          Rcpp_0.12.16
[25] xtable_1.8-2      promises_1.0.1    scales_1.0.0      backports_1.1.2
[29] jsonlite_1.5      mime_0.5          gridExtra_2.3     digest_0.6.15
[33] stringi_1.2.4     grid_3.5.0        rprojroot_1.3-2   tools_3.5.0
[37] magrittr_1.5      lazyeval_0.2.1    tibble_1.4.2      pkgconfig_2.0.2
[41] rsconnect_0.8.8   data.table_1.11.4 assertthat_0.2.0  R6_2.2.2
[45] compiler_3.5.0
>
`

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions