Skip to content

Bubble sizes out of order, when using a formula for the color/name attributes #2346

Open
@JElchison

Description

@JElchison

Hi folks, I'm seeing a strange effect on scatter plot marker sizes, when using a formula for the color (and name) attributes.

(edited to add more test cases)

library(plotly)
#> Loading required package: ggplot2
#> 
#> Attaching package: 'plotly'
#> The following object is masked from 'package:ggplot2':
#> 
#>     last_plot
#> The following object is masked from 'package:stats':
#> 
#>     filter
#> The following object is masked from 'package:graphics':
#> 
#>     layout

df <- data.frame(x = c(1, 2, 3, 4, 5),
                 y = c(1, 2, 3, 4, 5),
                 z = c(1, 2, 3, 4, 5))

# Expected output: marker sizes in order
plot_ly(df,
        x = ~x,
        y = ~y,
        type = "scatter",
        mode = "markers",
        marker = list(size = ~z,
                      sizeref = 0.1),
        color = ~z < 2,
        colors = c(I("green"), I("red")),
        text = ~paste0("z: ", z))

# df has correct data
df
#>   x y z
#> 1 1 1 1
#> 2 2 2 2
#> 3 3 3 3
#> 4 4 4 4
#> 5 5 5 5

# Buggy output: changing "color" formula threshold puts marker sizes out of order
plot_ly(df,
        x = ~x,
        y = ~y,
        type = "scatter",
        mode = "markers",
        marker = list(size = ~z,
                      sizeref = 0.1),
        color = ~z < 3,
        colors = c(I("green"), I("red")),
        text = ~paste0("z: ", z))

# df still has correct data
df
#>   x y z
#> 1 1 1 1
#> 2 2 2 2
#> 3 3 3 3
#> 4 4 4 4
#> 5 5 5 5

# Buggy output: static vector also puts marker sizes out of order
plot_ly(df,
        x = ~x,
        y = ~y,
        type = "scatter",
        mode = "markers",
        marker = list(size = ~z,
                      sizeref = 0.1),
        color = c(TRUE, TRUE, FALSE, FALSE, FALSE),
        colors = c(I("green"), I("red")),
        text = ~paste0("z: ", z))

# Buggy output: use ifelse with (TRUE, FALSE)
plot_ly(df,
        x = ~x,
        y = ~y,
        type = "scatter",
        mode = "markers",
        marker = list(size = ~z,
                      sizeref = 0.1),
        color = ~ifelse(z < 3, TRUE, FALSE),
        colors = c(I("green"), I("red")),
        text = ~paste0("z: ", z))

# Possible workaround: use ifelse with (1, 0)
plot_ly(df,
        x = ~x,
        y = ~y,
        type = "scatter",
        mode = "markers",
        marker = list(size = ~z,
                      sizeref = 0.1),
        color = ~ifelse(z < 3, 1, 0),
        colors = c(I("green"), I("red")),
        text = ~paste0("z: ", z))

# Buggy again when adding "name" field with formula
plot_ly(df,
        x = ~x,
        y = ~y,
        type = "scatter",
        mode = "markers",
        marker = list(size = ~z,
                      sizeref = 0.1),
        color = ~ifelse(z < 3, 1, 0),
        colors = c(I("green"), I("red")),
        name = ~ifelse(z < 3, "Red", "Green"),
        text = ~paste0("z: ", z))

Created on 2024-04-06 with reprex v2.1.0

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.3.3 (2024-02-29)
#>  os       Ubuntu 22.04.4 LTS
#>  system   x86_64, linux-gnu
#>  ui       X11
#>  language (EN)
#>  collate  en_US.UTF-8
#>  ctype    en_US.UTF-8
#>  tz       America/Indiana/Vevay
#>  date     2024-04-06
#>  pandoc   3.1.1 @ /usr/lib/rstudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date (UTC) lib source
#>  callr         3.7.5   2024-02-19 [1] CRAN (R 4.3.2)
#>  cli           3.6.2   2023-12-11 [1] CRAN (R 4.3.2)
#>  colorspace    2.1-0   2023-01-23 [1] CRAN (R 4.3.0)
#>  crosstalk     1.2.1   2023-11-23 [1] CRAN (R 4.3.2)
#>  curl          5.2.0   2023-12-08 [1] CRAN (R 4.3.2)
#>  data.table    1.15.0  2024-01-30 [1] CRAN (R 4.3.2)
#>  digest        0.6.34  2024-01-11 [1] CRAN (R 4.3.2)
#>  dplyr         1.1.4   2023-11-17 [1] CRAN (R 4.3.2)
#>  ellipsis      0.3.2   2021-04-29 [1] CRAN (R 4.3.0)
#>  evaluate      0.23    2023-11-01 [1] CRAN (R 4.3.2)
#>  fansi         1.0.6   2023-12-08 [1] CRAN (R 4.3.2)
#>  farver        2.1.1   2022-07-06 [1] CRAN (R 4.3.0)
#>  fastmap       1.1.1   2023-02-24 [1] CRAN (R 4.3.0)
#>  fs            1.6.3   2023-07-20 [1] CRAN (R 4.3.2)
#>  generics      0.1.3   2022-07-05 [1] CRAN (R 4.3.0)
#>  ggplot2     * 3.5.0   2024-02-23 [1] CRAN (R 4.3.2)
#>  glue          1.7.0   2024-01-09 [1] CRAN (R 4.3.2)
#>  gtable        0.3.4   2023-08-21 [1] CRAN (R 4.3.2)
#>  highr         0.10    2022-12-22 [1] CRAN (R 4.3.0)
#>  htmltools     0.5.7   2023-11-03 [1] CRAN (R 4.3.2)
#>  htmlwidgets   1.6.4   2023-12-06 [1] CRAN (R 4.3.2)
#>  httr          1.4.7   2023-08-15 [1] CRAN (R 4.3.2)
#>  jsonlite      1.8.8   2023-12-04 [1] CRAN (R 4.3.2)
#>  knitr         1.45    2023-10-30 [1] CRAN (R 4.3.2)
#>  lazyeval      0.2.2   2019-03-15 [1] CRAN (R 4.3.0)
#>  lifecycle     1.0.4   2023-11-07 [1] CRAN (R 4.3.2)
#>  magrittr      2.0.3   2022-03-30 [1] CRAN (R 4.3.0)
#>  munsell       0.5.0   2018-06-12 [1] CRAN (R 4.3.0)
#>  pillar        1.9.0   2023-03-22 [1] CRAN (R 4.3.0)
#>  pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.3.0)
#>  plotly      * 4.10.4  2024-01-13 [1] CRAN (R 4.3.2)
#>  processx      3.8.3   2023-12-10 [1] CRAN (R 4.3.2)
#>  ps            1.7.6   2024-01-18 [1] CRAN (R 4.3.2)
#>  purrr         1.0.2   2023-08-10 [1] CRAN (R 4.3.2)
#>  R.cache       0.16.0  2022-07-21 [1] CRAN (R 4.3.1)
#>  R.methodsS3   1.8.2   2022-06-13 [1] CRAN (R 4.3.1)
#>  R.oo          1.26.0  2024-01-24 [1] CRAN (R 4.3.2)
#>  R.utils       2.12.3  2023-11-18 [1] CRAN (R 4.3.2)
#>  R6            2.5.1   2021-08-19 [1] CRAN (R 4.3.0)
#>  reprex        2.1.0   2024-01-11 [1] CRAN (R 4.3.3)
#>  rlang         1.1.3   2024-01-10 [1] CRAN (R 4.3.2)
#>  rmarkdown     2.25    2023-09-18 [1] CRAN (R 4.3.2)
#>  rstudioapi    0.15.0  2023-07-07 [1] CRAN (R 4.3.2)
#>  scales        1.3.0   2023-11-28 [1] CRAN (R 4.3.2)
#>  sessioninfo   1.2.2   2021-12-06 [1] CRAN (R 4.3.0)
#>  styler        1.10.2  2023-08-29 [1] CRAN (R 4.3.1)
#>  tibble        3.2.1   2023-03-20 [1] CRAN (R 4.3.0)
#>  tidyr         1.3.1   2024-01-24 [1] CRAN (R 4.3.2)
#>  tidyselect    1.2.0   2022-10-10 [1] CRAN (R 4.3.0)
#>  utf8          1.2.4   2023-10-22 [1] CRAN (R 4.3.2)
#>  vctrs         0.6.5   2023-12-01 [1] CRAN (R 4.3.2)
#>  viridisLite   0.4.2   2023-05-02 [1] CRAN (R 4.3.0)
#>  webshot       0.5.5   2023-06-26 [1] CRAN (R 4.3.2)
#>  withr         3.0.0   2024-01-16 [1] CRAN (R 4.3.2)
#>  xfun          0.42    2024-02-08 [1] CRAN (R 4.3.2)
#>  xml2          1.3.6   2023-12-04 [1] CRAN (R 4.3.2)
#>  yaml          2.3.8   2023-12-11 [1] CRAN (R 4.3.2)
#> 
#>  [1] /home/jonathan/R/x86_64-pc-linux-gnu-library/4.3
#>  [2] /usr/local/lib/R/site-library
#>  [3] /usr/lib/R/site-library
#>  [4] /usr/lib/R/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────

Bad color formulas are:

  • z < 3
  • z < 4
  • z < 5

The results from z < 2 may also be incorrect ... they're just indiscernible given my example.

This behavior causes significant skewing/confusion for bubble plots of any size.

Thanks for reading!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions