coolbutuseless / ggsvg

Use SVG images as ggplot points
https://coolbutuseless.github.io/package/ggsvg/
Other
138 stars 5 forks source link

cars icon example - not all rendered #1

Closed timelyportfolio closed 2 years ago

timelyportfolio commented 2 years ago

I am so excited for this new functionality. All seems to work correctly until I attempt the cars example where only a subset of the cars are rendered. Each time I rerun the code I get a different subset of the data. No errors or warnings are produced.

library(ggsvg)
library(ggplot2)

car_url <- 'https://www.svgrepo.com/download/114837/car.svg'
car_svg <- paste(readLines(car_url), collapse = "\n")
ggplot(mtcars) + 
  geom_point_svg(
    aes(mpg, wt),
    svg = car_svg,
    size = 8
  ) + 
  theme_bw()

image

I believe all the packages are on their most recent version. I get the same result regardless of graphics device that I choose.

- Session info --------------------------------------------------------------------------------------------------------
 setting  value
 version  R version 4.0.5 (2021-03-31)
 os       Windows 10 x64 (build 19043)
 system   x86_64, mingw32
 ui       RStudio
 language (EN)
 collate  English_United States.1252
 ctype    English_United States.1252
 tz       America/Chicago
 date     2021-12-27
 rstudio  1.4.1106 Tiger Daylily (desktop)
 pandoc   NA

- Packages ------------------------------------------------------------------------------------------------------------
 package     * version date (UTC) lib source
 assertthat    0.2.1   2019-03-21 [1] CRAN (R 4.0.5)
 cachem        1.0.4   2021-02-13 [1] CRAN (R 4.0.5)
 callr         3.7.0   2021-04-20 [1] CRAN (R 4.0.5)
 cli           3.1.0   2021-10-27 [1] CRAN (R 4.0.5)
 colorspace    2.0-2   2021-06-24 [1] CRAN (R 4.0.5)
 crayon        1.4.2   2021-10-29 [1] CRAN (R 4.0.5)
 cssparser     0.1.1   2021-12-27 [1] Github (coolbutuseless/cssparser@a5a3765)
 curl          4.3     2019-12-02 [1] CRAN (R 4.0.5)
 DBI           1.1.1   2021-01-15 [1] CRAN (R 4.0.5)
 desc          1.4.0   2021-09-28 [1] CRAN (R 4.0.5)
 devtools      2.4.3   2021-11-30 [1] CRAN (R 4.0.5)
 digest        0.6.29  2021-12-01 [1] CRAN (R 4.0.5)
 dplyr         1.0.5   2021-03-05 [1] CRAN (R 4.0.5)
 ellipsis      0.3.2   2021-04-29 [1] CRAN (R 4.0.5)
 fansi         0.5.0   2021-05-25 [1] CRAN (R 4.0.5)
 fastmap       1.1.0   2021-01-25 [1] CRAN (R 4.0.5)
 fs            1.5.0   2020-07-31 [1] CRAN (R 4.0.5)
 generics      0.1.0   2020-10-31 [1] CRAN (R 4.0.5)
 ggplot2     * 3.3.5   2021-06-25 [1] CRAN (R 4.0.5)
 ggsvg       * 0.1.1   2021-12-27 [1] Github (coolbutuseless/ggsvg@380a202)
 glue          1.6.0   2021-12-17 [1] CRAN (R 4.0.5)
 gtable        0.3.0   2019-03-25 [1] CRAN (R 4.0.5)
 labeling      0.4.2   2020-10-20 [1] CRAN (R 4.0.3)
 lifecycle     1.0.1   2021-09-24 [1] CRAN (R 4.0.5)
 magrittr      2.0.1   2020-11-17 [1] CRAN (R 4.0.5)
 memoise       2.0.1   2021-11-26 [1] CRAN (R 4.0.5)
 munsell       0.5.0   2018-06-12 [1] CRAN (R 4.0.5)
 pillar        1.6.4   2021-10-18 [1] CRAN (R 4.0.5)
 pkgbuild      1.2.0   2020-12-15 [1] CRAN (R 4.0.5)
 pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.0.5)
 pkgload       1.2.4   2021-11-30 [1] CRAN (R 4.0.5)
 prettyunits   1.1.1   2020-01-24 [1] CRAN (R 4.0.5)
 processx      3.5.1   2021-04-04 [1] CRAN (R 4.0.5)
 ps            1.6.0   2021-02-28 [1] CRAN (R 4.0.5)
 purrr         0.3.4   2020-04-17 [1] CRAN (R 4.0.5)
 R6            2.5.1   2021-08-19 [1] CRAN (R 4.0.5)
 remotes       2.3.0   2021-04-01 [1] CRAN (R 4.0.5)
 rlang         0.4.12  2021-10-18 [1] CRAN (R 4.0.5)
 rprojroot     2.0.2   2020-11-15 [1] CRAN (R 4.0.5)
 rstudioapi    0.13    2020-11-12 [1] CRAN (R 4.0.5)
 scales        1.1.1   2020-05-11 [1] CRAN (R 4.0.5)
 selectr       0.4-2   2019-11-20 [1] CRAN (R 4.0.5)
 sessioninfo   1.2.2   2021-12-06 [1] CRAN (R 4.0.5)
 stringi       1.7.6   2021-11-29 [1] CRAN (R 4.0.5)
 stringr       1.4.0   2019-02-10 [1] CRAN (R 4.0.5)
 svgparser     0.1.2   2021-12-27 [1] Github (coolbutuseless/svgparser@eed57af)
 testthat      3.1.1   2021-12-03 [1] CRAN (R 4.0.5)
 tibble        3.1.6   2021-11-07 [1] CRAN (R 4.0.5)
 tidyselect    1.1.0   2020-05-11 [1] CRAN (R 4.0.5)
 usethis       2.1.5   2021-12-09 [1] CRAN (R 4.0.5)
 utf8          1.2.2   2021-07-24 [1] CRAN (R 4.0.5)
 vctrs         0.3.8   2021-04-29 [1] CRAN (R 4.0.5)
 withr         2.4.3   2021-11-30 [1] CRAN (R 4.0.5)
 xml2          1.3.3   2021-11-30 [1] CRAN (R 4.0.5)
 yaml          2.2.1   2020-02-01 [1] CRAN (R 4.0.4)
-----------------------------------------------------------------------------------------------------------------------
timelyportfolio commented 2 years ago

I just realized I am on a computer so updated R to 4.1.2. Unfortunately, the problem still occurs.

coolbutuseless commented 2 years ago

I don't see this on MacOS, but I have an inkling on why this might be happening.

Could you try an earlier version (prior to 'fastpath' being added) and see if this still happens?

Try this commit: 594e6b965ecf018c71fc21af07dbebda661e29cb

Current theory:

timelyportfolio commented 2 years ago

@coolbutuseless Using https://github.com/coolbutuseless/ggsvg/commit/594e6b965ecf018c71fc21af07dbebda661e29cb fixes the issue. Makes sense and thanks for the explanation. Let me know if I can help debug or test.

timelyportfolio commented 2 years ago

To add another example to the mix, here is what I was trying to achieve when I discovered the problem.

library(ggsvg)
library(ggplot2)

arrow_url <- "https://www.svgrepo.com/download/102966/arrow-pointing-to-up.svg"
arrow_svg <- paste(readLines(arrow_url), collapse = "\n")

# add rotate transform to be populated from data
# more robust with xml2 but just use gsub for now
arrow_svg_with_rotate <- gsub(
  x = arrow_svg,
  pattern = "viewBox=",
  replacement = "transform = 'rotate({{rotate}}, 16, 16)' viewBox="
)

dat <- data.frame(
  x = 1:20,
  y = 1:20,
  rot = seq.int(from = 0, to = 360, length.out = 10)
)

ggplot(dat) + 
  geom_point_svg(
    mapping = aes(
      x = x,
      y = y,
      rotate = rot
    ),
    defaults = list(rotate = 0),
    svg = arrow_svg_with_rotate,
    size = 5
  ) +
  # to test rotate origin; used 16,16 since half of viewBox 0,0,32,32
  # geom_point_svg(
  #   mapping = aes(
  #     x = x,
  #     y = y
  #   ),
  #   svg = arrow_svg,
  #   size = 5
  # ) +
  theme_bw()

image

coolbutuseless commented 2 years ago

Can you try: d765be2adace76269dcf0cbb279774e19fb0d5d8

I added code in the fastpath such that if SVG is the same for all points:

timelyportfolio commented 2 years ago

@coolbutuseless d765be2adace76269dcf0cbb279774e19fb0d5d8 works on my end.

coolbutuseless commented 2 years ago

Excellent. Closing.