davidgohel / ggiraph

make 'ggplot' graphics interactive

Home Page:https://davidgohel.github.io/ggiraph

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Incorrect shape and color of interactive points when shapes with lines are used

nicokist opened this issue · comments

Hello,

Thanks for this amazing package, I've really been able to have a lot of fun with it!

I recently come across some surprising behavior: when tooltip is added to the aesthetics some of the points seem to no longer be colored correctly. Please find a reprex attached:

This problem goes away once the tooltip is removed from the aesthetics.

library(tidyverse)
library(ggiraph)

dat <- structure(list(x = c(
  0.670700732152909, 0.94666058011353, 0.271530218888074,
  0.28272072202526, 0.950930949067697, 0.774316143942997, 0.951495578512549,
  0.277329422067851, 0.424100575968623, 0.454079832881689
), y = c(
  0.29804180865176,
  0.434860641835257, 0.568946241633967, 0.701763016404584, 0.12649045791477,
  0.37094813096337, 0.728182743070647, 0.256948527181521, 0.562205084599555,
  0.570892055518925
), genus = c(
  2L, 1L, 2L, 1L, 1L, 1L, 2L, 1L,
  2L, 2L
), genus_species = c(
  "2_1", "1_2", "2_2", "1_1", "1_1",
  "1_1", "2_1", "1_2", "2_2", "2_2"
)), row.names = c(NA, -10L), class = c(
  "tbl_df",
  "tbl", "data.frame"
))

dat |>
  mutate(
    genus = factor(genus),
    genus_species = factor(genus_species)
  ) |>
  ggplot() +
    aes(x = x, y = y, col = `genus`, shape = genus_species, tooltip = genus_species) +
    geom_point()

Left bottom point (~0.25, ~0.25) correct color and shape

dat |>
    mutate(
        genus = factor(genus),
        genus_species = factor(genus_species)
    ) |>
    ggplot() +
    aes(x = x, y = y, col = `genus`, shape = genus_species, tooltip = genus_species) +
    geom_point_interactive()


Left bottom point (~0.25, ~0.25) incorrect color

sessionInfo()
#> R version 4.2.2 (2022-10-31)
#> Platform: aarch64-apple-darwin20 (64-bit)
#> Running under: macOS Ventura 13.1
#> 
#> Matrix products: default
#> BLAS:   /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRblas.0.dylib
#> LAPACK: /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRlapack.dylib
#> 
#> locale:
#> [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#>  [1] ggiraph_0.8.5   forcats_0.5.2   stringr_1.4.1   dplyr_1.0.10   
#>  [5] purrr_0.3.5     readr_2.1.3     tidyr_1.2.1     tibble_3.1.8   
#>  [9] ggplot2_3.4.0   tidyverse_1.3.2
#> 
#> loaded via a namespace (and not attached):
#>  [1] Rcpp_1.0.9          lubridate_1.9.0     assertthat_0.2.1   
#>  [4] digest_0.6.30       utf8_1.2.2          R6_2.5.1           
#>  [7] cellranger_1.1.0    backports_1.4.1     reprex_2.0.2       
#> [10] evaluate_0.18       httr_1.4.4          highr_0.9          
#> [13] pillar_1.8.1        rlang_1.0.6         uuid_1.1-0         
#> [16] googlesheets4_1.0.1 readxl_1.4.1        rstudioapi_0.14    
#> [19] R.utils_2.12.2      R.oo_1.25.0         rmarkdown_2.18     
#> [22] styler_1.8.1        labeling_0.4.2      googledrive_2.0.0  
#> [25] htmlwidgets_1.5.4   munsell_0.5.0       broom_1.0.1        
#> [28] compiler_4.2.2      modelr_0.1.10       xfun_0.35          
#> [31] systemfonts_1.0.4   pkgconfig_2.0.3     htmltools_0.5.3    
#> [34] tidyselect_1.2.0    fansi_1.0.3         crayon_1.5.2       
#> [37] tzdb_0.3.0          dbplyr_2.2.1        withr_2.5.0        
#> [40] R.methodsS3_1.8.2   grid_4.2.2          jsonlite_1.8.3     
#> [43] gtable_0.3.1        lifecycle_1.0.3     DBI_1.1.3          
#> [46] magrittr_2.0.3      scales_1.2.1        cli_3.4.1          
#> [49] stringi_1.7.8       farver_2.1.1        fs_1.5.2           
#> [52] xml2_1.3.3          ellipsis_0.3.2      generics_0.1.3     
#> [55] vctrs_0.5.1         tools_4.2.2         R.cache_0.16.0     
#> [58] glue_1.6.2          hms_1.1.2           fastmap_1.1.0      
#> [61] yaml_2.3.6          timechange_0.1.1    colorspace_2.0-3   
#> [64] gargle_1.2.1        rvest_1.0.3         knitr_1.41         
#> [67] haven_2.5.1

Created on 2023-01-20 with reprex v2.0.2

Interestingly the colors in the plot (but not in the legend) appear to change when the order of the data frame changes, even when the factor is defined before the shuffling.

Running the ggplot chain below several times after creating dat2 will show the geom_point changing colors: it seems to be strange interaction of tooltip, shape, and color.

dat2 <- iris |>
  mutate(species2 = paste0(Species, sample.int(n = 2)))

dat2 |>
  sample_frac() |>
  ggplot() +
  aes(x = Sepal.Width, y = Sepal.Length, col = Species, shape = species2) +
  geom_point_interactive(tooltip = "a tooltip")

Thank you for reporting this and for the time you spent on making this case reproducible, we will try to fix soon!

Another small reprex for similar issue. Note the "Currently does not work"-line. Works well without it.

structure(list(
  .variable_label = structure(c(b_1 = 1L, b_1 = 1L, b_1 = 1L, b_2 = 2L, b_2 = 2L, b_2 = 2L), 
                              levels = c("Bejing", "Brussels"), class = c("ordered", "factor")), 
.count = c(55L, 37L, 8L, 40L, 48L, 12L), 
.data_label = c("55", "37", "8", "40", "48", "12"), 
.category = structure(c(1L, 2L, 3L, 1L, 2L, 3L), 
                      levels = c("Not at all", "A bit", "A lot"), class = "factor")), 
row.names = c(NA, -6L), class = c("data.frame")) |>
      ggplot2::ggplot(
        mapping = ggplot2::aes(
          y = .count,
          x = .variable_label,
          fill = .category,
          label = .data_label),
        cumulative = TRUE) +
      ggiraph::geom_col_interactive(
        mapping = ggplot2::aes(tooltip = .data_label), # Currently does not work
        position = ggplot2::position_stack()) +
      ggiraph::geom_text_interactive(position = ggplot2::position_stack(vjust = .5))

Created on 2023-03-21 with reprex v2.0.2

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.2.2 (2022-10-31 ucrt)
#>  os       Windows 10 x64 (build 22621)
#>  system   x86_64, mingw32
#>  ui       RTerm
#>  language (EN)
#>  collate  nb.utf8
#>  ctype    nb.utf8
#>  tz       Europe/Berlin
#>  date     2023-03-21
#>  pandoc   2.19.2 @ C:/Program Files/RStudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date (UTC) lib source
#>  cli           3.6.0   2023-01-09 [1] CRAN (R 4.2.2)
#>  colorspace    2.1-0   2023-01-23 [1] CRAN (R 4.2.2)
#>  curl          5.0.0   2023-01-12 [1] CRAN (R 4.2.2)
#>  digest        0.6.31  2022-12-11 [1] CRAN (R 4.2.2)
#>  dplyr         1.1.0   2023-01-29 [1] CRAN (R 4.2.2)
#>  evaluate      0.20    2023-01-17 [1] CRAN (R 4.2.2)
#>  fansi         1.0.4   2023-01-22 [1] CRAN (R 4.2.2)
#>  farver        2.1.1   2022-07-06 [1] CRAN (R 4.2.1)
#>  fastmap       1.1.1   2023-02-24 [1] CRAN (R 4.2.2)
#>  fs            1.6.1   2023-02-06 [1] CRAN (R 4.2.2)
#>  generics      0.1.3   2022-07-05 [1] CRAN (R 4.2.1)
#>  ggiraph       0.8.7   2023-03-17 [1] CRAN (R 4.2.3)
#>  ggplot2       3.4.1   2023-02-10 [1] CRAN (R 4.2.2)
#>  glue          1.6.2   2022-02-24 [1] CRAN (R 4.1.2)
#>  gtable        0.3.2   2023-03-17 [1] CRAN (R 4.2.3)
#>  highr         0.10    2022-12-22 [1] CRAN (R 4.2.2)
#>  htmltools     0.5.4   2022-12-07 [1] CRAN (R 4.2.2)
#>  htmlwidgets   1.6.2   2023-03-17 [1] CRAN (R 4.2.3)
#>  httr          1.4.5   2023-02-24 [1] CRAN (R 4.2.2)
#>  knitr         1.42    2023-01-25 [1] CRAN (R 4.2.2)
#>  labeling      0.4.2   2020-10-20 [1] CRAN (R 4.0.3)
#>  lifecycle     1.0.3   2022-10-07 [1] CRAN (R 4.2.1)
#>  magrittr      2.0.3   2022-03-30 [1] CRAN (R 4.1.3)
#>  mime          0.12    2021-09-28 [1] CRAN (R 4.1.1)
#>  munsell       0.5.0   2018-06-12 [1] CRAN (R 4.0.3)
#>  pillar        1.8.1   2022-08-19 [1] CRAN (R 4.2.1)
#>  pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.0.3)
#>  purrr         1.0.1   2023-01-10 [1] CRAN (R 4.2.2)
#>  R.cache       0.16.0  2022-07-21 [1] CRAN (R 4.2.1)
#>  R.methodsS3   1.8.2   2022-06-13 [1] CRAN (R 4.2.0)
#>  R.oo          1.25.0  2022-06-12 [1] CRAN (R 4.2.0)
#>  R.utils       2.12.2  2022-11-11 [1] CRAN (R 4.2.1)
#>  R6            2.5.1   2021-08-19 [1] CRAN (R 4.1.1)
#>  Rcpp          1.0.10  2023-01-22 [1] CRAN (R 4.2.2)
#>  reprex        2.0.2   2022-08-17 [1] CRAN (R 4.2.1)
#>  rlang         1.1.0   2023-03-14 [1] CRAN (R 4.2.2)
#>  rmarkdown     2.20    2023-01-19 [1] CRAN (R 4.2.2)
#>  rstudioapi    0.14    2022-08-22 [1] CRAN (R 4.2.1)
#>  scales        1.2.1   2022-08-20 [1] CRAN (R 4.2.1)
#>  sessioninfo   1.2.2   2021-12-06 [1] CRAN (R 4.1.2)
#>  styler        1.9.1   2023-03-04 [1] CRAN (R 4.2.2)
#>  systemfonts   1.0.4   2022-02-11 [1] CRAN (R 4.1.2)
#>  tibble        3.2.1   2023-03-20 [1] CRAN (R 4.2.2)
#>  tidyselect    1.2.0   2022-10-10 [1] CRAN (R 4.2.1)
#>  utf8          1.2.3   2023-01-31 [1] CRAN (R 4.2.2)
#>  uuid          1.1-0   2022-04-19 [1] CRAN (R 4.2.0)
#>  vctrs         0.6.0   2023-03-16 [1] CRAN (R 4.2.2)
#>  withr         2.5.0   2022-03-03 [1] CRAN (R 4.1.2)
#>  xfun          0.37    2023-01-31 [1] CRAN (R 4.2.2)
#>  xml2          1.3.3   2021-11-30 [1] CRAN (R 4.1.2)
#>  yaml          2.3.7   2023-01-23 [1] CRAN (R 4.2.2)
#> 
#>  [1] C:/Users/py128/OneDrive - NIFU/R
#>  [2] C:/Program Files/R/R-4.2.2/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────

Thanks @nicokist . The bug occurs when some points have shapes with lines (like a cross or an X). Created a fix and it will be included in the next version.

Thanks @sda030 . What you describe is unrelated with the original issue.
The problem is that in that case you should explicitly specify a group aesthetic:

mapping = ggplot2::aes(tooltip = .data_label, group = .category)

Adding an interactive aesthetic like tooltip, sometimes alters the original grouping that ggplot2 does implicitly, even if you don't specify the group aesthetic. In those cases you must specify the group aesthetic in order to get the graph correctly rendered.