3D scatter plot not drawn by z-axis order when alpha != 1
When creating a 3D scatter plot, the dots should be drawn in the order of depth, so drawing the furthest dot first, to get the right occlusion. This works well when alpha = 1, but when alpha < 1 the dots seem to be drawn in order of categories instead.
Here's a simple example. If you rotate it so the green dot is at the forefront, it is nevertheless occluded by the blue dot:
library(plotly)
plot_ly(data = data.frame(x = c(1, 1),
y = c(1, 2),
z = c(1, 1),
class = c("A", "B")),
x = ~x,
y = ~y,
z = ~z,
color = ~class,
alpha = 0.9)
This especially problematic when you have a lot of dots, and a cluster of nearby dots is completely hidden by a faraway cluster of dots of a different color.
> sessionInfo()
R version 4.0.3 (2020-10-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)
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.0 ggplot2_3.3.5
loaded via a namespace (and not attached):
[1] Rcpp_1.0.7 pillar_1.6.4 compiler_4.0.3 later_1.3.0 RColorBrewer_1.1-2 tools_4.0.3 digest_0.6.28
[8] viridisLite_0.4.0 jsonlite_1.7.2 lifecycle_1.0.1 tibble_3.1.5 gtable_0.3.0 pkgconfig_2.0.3 rlang_0.4.11
[15] shiny_1.7.1 DBI_1.1.1 crosstalk_1.1.1 yaml_2.2.1 fastmap_1.1.0 httr_1.4.2 withr_2.4.2
[22] dplyr_1.0.7 htmlwidgets_1.5.4 generics_0.1.0 vctrs_0.3.8 grid_4.0.3 tidyselect_1.1.1 data.table_1.14.2
[29] glue_1.4.2 R6_2.5.1 fansi_0.5.0 farver_2.1.0 tidyr_1.1.4 purrr_0.3.4 magrittr_2.0.1
[36] scales_1.1.1 promises_1.2.0.1 ellipsis_0.3.2 htmltools_0.5.2 assertthat_0.2.1 mime_0.12 colorspace_2.0-2
[43] xtable_1.8-4 httpuv_1.6.3 utf8_1.2.2 lazyeval_0.2.2 munsell_0.5.0 crayon_1.4.1
Pro tip, search if the issue exists before wasting time on a reprex 😂. I would add that this is an especially prevalent bug since the default is to render 3D bubbles and apply transparency when using the size argument.
library(plotly)
d<-rbind(
data.frame(x=3,y=3,z=3,g='A')
,data.frame(x=2,y=2,z=2,g='B')
,data.frame(x=1,y=1,z=1,g='C')
)
p<-plot_ly(d,x=~x,y=~y,z=~z,color=~g,size=~I(pi*1000*(3:1)^2))
# Fine
p %>% add_markers(marker=list(opacity=1))

# Far points render in front of near points
p %>% add_markers(marker=list(opacity=.99))

Created on 2022-01-31 by the reprex package (v2.0.1)
Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#> setting value
#> version R version 4.1.2 (2021-11-01)
#> os macOS Monterey 12.1
#> system aarch64, darwin20.6.0
#> ui unknown
#> language (EN)
#> collate en_US.UTF-8
#> ctype en_US.UTF-8
#> tz America/Los_Angeles
#> date 2022-01-31
#> pandoc 2.14.0.3 @ /Applications/RStudio.app/Contents/MacOS/pandoc/ (via rmarkdown)
#>
#> ─ Packages ───────────────────────────────────────────────────────────────────
#> package * version date (UTC) lib source
#> backports 1.2.1 2020-12-09 [1] CRAN (R 4.1.1)
#> callr 3.7.0 2021-04-20 [1] CRAN (R 4.1.1)
#> cli 3.1.0 2021-10-27 [1] CRAN (R 4.1.1)
#> colorspace 2.0-2 2021-06-24 [1] CRAN (R 4.1.1)
#> crayon 1.4.2 2021-10-29 [1] CRAN (R 4.1.1)
#> crosstalk 1.1.1 2021-01-12 [1] CRAN (R 4.1.1)
#> curl 4.3.2 2021-06-23 [1] CRAN (R 4.1.1)
#> data.table 1.14.3 2021-10-04 [1] local
#> digest 0.6.29 2021-12-01 [1] CRAN (R 4.1.2)
#> dplyr 1.0.7 2021-06-18 [1] CRAN (R 4.1.1)
#> ellipsis 0.3.2 2021-04-29 [1] CRAN (R 4.1.1)
#> evaluate 0.14 2019-05-28 [1] CRAN (R 4.1.1)
#> fansi 1.0.0 2022-01-10 [1] CRAN (R 4.1.2)
#> farver 2.1.0 2021-02-28 [1] CRAN (R 4.1.1)
#> fastmap 1.1.0 2021-01-25 [1] CRAN (R 4.1.1)
#> fs 1.5.2 2021-12-08 [1] CRAN (R 4.1.2)
#> generics 0.1.1 2021-10-25 [1] CRAN (R 4.1.1)
#> ggplot2 * 3.3.5 2021-06-25 [1] CRAN (R 4.1.1)
#> glue 1.6.0 2021-12-17 [1] CRAN (R 4.1.2)
#> gtable 0.3.0 2019-03-25 [1] CRAN (R 4.1.1)
#> highr 0.9 2021-04-16 [1] CRAN (R 4.1.1)
#> htmltools 0.5.2 2021-08-25 [1] CRAN (R 4.1.1)
#> htmlwidgets 1.5.4 2021-09-08 [1] CRAN (R 4.1.1)
#> httr 1.4.2 2020-07-20 [1] CRAN (R 4.1.1)
#> jsonlite 1.7.2 2020-12-09 [1] CRAN (R 4.1.1)
#> knitr 1.37 2021-12-16 [1] CRAN (R 4.1.2)
#> lazyeval 0.2.2 2019-03-15 [1] CRAN (R 4.1.1)
#> lifecycle 1.0.1 2021-09-24 [1] CRAN (R 4.1.1)
#> magrittr 2.0.2 2022-01-26 [1] CRAN (R 4.1.2)
#> mime 0.12 2021-09-28 [1] CRAN (R 4.1.1)
#> munsell 0.5.0 2018-06-12 [1] CRAN (R 4.1.1)
#> pillar 1.6.4 2021-10-18 [1] CRAN (R 4.1.1)
#> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.1.1)
#> plotly * 4.10.0 2021-10-09 [1] CRAN (R 4.1.2)
#> processx 3.5.2 2021-04-30 [1] CRAN (R 4.1.1)
#> ps 1.6.0 2021-02-28 [1] CRAN (R 4.1.1)
#> purrr 0.3.4 2020-04-17 [1] CRAN (R 4.1.1)
#> R.cache 0.15.0 2021-04-30 [1] CRAN (R 4.1.1)
#> R.methodsS3 1.8.1 2020-08-26 [1] CRAN (R 4.1.1)
#> R.oo 1.24.0 2020-08-26 [1] CRAN (R 4.1.1)
#> R.utils 2.11.0 2021-09-26 [1] CRAN (R 4.1.1)
#> R6 2.5.1 2021-08-19 [1] CRAN (R 4.1.1)
#> RColorBrewer 1.1-2 2014-12-07 [1] CRAN (R 4.1.1)
#> reprex 2.0.1 2021-08-05 [1] CRAN (R 4.1.2)
#> rlang 0.4.12 2021-10-18 [1] CRAN (R 4.1.1)
#> rmarkdown 2.11 2021-09-14 [1] CRAN (R 4.1.1)
#> rstudioapi 0.13 2020-11-12 [1] CRAN (R 4.1.1)
#> scales 1.1.1 2020-05-11 [1] CRAN (R 4.1.1)
#> sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.1.2)
#> stringi 1.7.6 2021-11-29 [1] CRAN (R 4.1.2)
#> stringr 1.4.0 2019-02-10 [1] CRAN (R 4.1.1)
#> styler 1.6.2 2021-09-23 [1] CRAN (R 4.1.1)
#> tibble 3.1.6 2021-11-07 [1] CRAN (R 4.1.2)
#> tidyr 1.1.4 2021-09-27 [1] CRAN (R 4.1.1)
#> tidyselect 1.1.1 2021-04-30 [1] CRAN (R 4.1.1)
#> utf8 1.2.2 2021-07-24 [1] CRAN (R 4.1.1)
#> vctrs 0.3.8 2021-04-29 [1] CRAN (R 4.1.1)
#> viridisLite 0.4.0 2021-04-13 [1] CRAN (R 4.1.1)
#> webshot 0.5.2 2019-11-22 [1] CRAN (R 4.1.1)
#> withr 2.4.3 2021-11-30 [1] CRAN (R 4.1.2)
#> xfun 0.29 2021-12-14 [1] CRAN (R 4.1.2)
#> xml2 1.3.3 2021-11-30 [1] CRAN (R 4.1.2)
#> yaml 2.2.1 2020-02-01 [1] CRAN (R 4.1.1)
#>
#> [1] /opt/homebrew/lib/R/4.1/site-library
#> [2] /opt/homebrew/Cellar/r/4.1.2/lib/R/library
#>
#> ──────────────────────────────────────────────────────────────────────────────
cross referencing an issue on the js repo https://github.com/plotly/plotly.js/issues/1267