JohnCoene / echarts4r

🐳 ECharts 5 for R
http://echarts4r.john-coene.com/
Other
585 stars 82 forks source link

Problem chaining e_text_g for multiple text placement #610

Open dernesa opened 6 months ago

dernesa commented 6 months ago

I encountered an issue when attempting to use e_text_g() to place text on both the left and right sides of a plot. While individually placing text on either side works well, attempting to combine both commands results in failure.

library(echarts4r)

df <- data.frame(
  x = seq(10),
  y = rnorm(10, 10, 3)
)

Created on 2024-01-03 with reprex v2.0.2

Session info ``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.3.2 (2023-10-31) #> os macOS Sonoma 14.2.1 #> system aarch64, darwin20 #> ui X11 #> language (EN) #> collate en_US.UTF-8 #> ctype en_US.UTF-8 #> tz Europe/Berlin #> date 2024-01-03 #> pandoc 3.1.8 @ /opt/local/bin/ (via rmarkdown) #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date (UTC) lib source #> cli 3.6.2 2023-12-11 [1] CRAN (R 4.3.1) #> digest 0.6.33 2023-07-07 [1] CRAN (R 4.3.0) #> dplyr 1.1.4 2023-11-17 [1] CRAN (R 4.3.1) #> echarts4r * 0.4.5 2023-06-16 [1] CRAN (R 4.3.0) #> ellipsis 0.3.2 2021-04-29 [1] CRAN (R 4.3.0) #> evaluate 0.23 2023-11-01 [1] CRAN (R 4.3.1) #> fansi 1.0.6 2023-12-08 [1] CRAN (R 4.3.1) #> 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.0) #> generics 0.1.3 2022-07-05 [1] CRAN (R 4.3.0) #> glue 1.6.2 2022-02-24 [1] CRAN (R 4.3.0) #> htmltools 0.5.7 2023-11-03 [1] CRAN (R 4.3.1) #> htmlwidgets 1.6.4 2023-12-06 [1] CRAN (R 4.3.1) #> httpuv 1.6.13 2023-12-06 [1] CRAN (R 4.3.1) #> knitr 1.45 2023-10-30 [1] CRAN (R 4.3.1) #> later 1.3.2 2023-12-06 [1] CRAN (R 4.3.1) #> lifecycle 1.0.4 2023-11-07 [1] CRAN (R 4.3.1) #> magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.3.0) #> mime 0.12 2021-09-28 [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) #> promises 1.2.1 2023-08-10 [1] CRAN (R 4.3.0) #> purrr 1.0.2 2023-08-10 [1] CRAN (R 4.3.0) #> R.cache 0.16.0 2022-07-21 [1] CRAN (R 4.3.0) #> R.methodsS3 1.8.2 2022-06-13 [1] CRAN (R 4.3.0) #> R.oo 1.25.0 2022-06-12 [1] CRAN (R 4.3.0) #> R.utils 2.12.3 2023-11-18 [1] CRAN (R 4.3.1) #> R6 2.5.1 2021-08-19 [1] CRAN (R 4.3.0) #> Rcpp 1.0.11 2023-07-06 [1] CRAN (R 4.3.0) #> reprex 2.0.2 2022-08-17 [1] CRAN (R 4.3.0) #> rlang 1.1.2 2023-11-04 [1] CRAN (R 4.3.1) #> rmarkdown 2.25 2023-09-18 [1] CRAN (R 4.3.1) #> rstudioapi 0.15.0 2023-07-07 [1] CRAN (R 4.3.0) #> sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.3.0) #> shiny 1.8.0 2023-11-17 [1] CRAN (R 4.3.1) #> styler 1.10.2 2023-08-29 [1] CRAN (R 4.3.0) #> tibble 3.2.1 2023-03-20 [1] CRAN (R 4.3.0) #> 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.1) #> vctrs 0.6.5 2023-12-01 [1] CRAN (R 4.3.2) #> withr 2.5.2 2023-10-30 [1] CRAN (R 4.3.1) #> xfun 0.41 2023-11-01 [1] CRAN (R 4.3.1) #> xtable 1.8-4 2019-04-21 [1] CRAN (R 4.3.0) #> yaml 2.3.8 2023-12-11 [1] CRAN (R 4.3.1) #> #> [1] /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library #> #> ────────────────────────────────────────────────────────────────────────────── ```

Placing text on the left side works as expected:

df |> 
  e_charts(x) |> 
  e_scatter(y) |> 
  echarts4r::e_text_g(
    style = list(text = "left",
                 fontSize = 30,
                 fill = "red"),
    left = "10%",
    top = "7%"
  )

echarts_left

And on the right side:

df |> 
  e_charts(x) |> 
  e_scatter(y) |> 
  echarts4r::e_text_g(
    style = list(text = "right",
                 fontSize = 30,
                 fill = "red"),
    right = "10%",
    top = "7%"
  )

echarts_right

However, when attempting to place text on both sides by chaining both commands, an issue arises:

df |> 
  e_charts(x) |> 
  e_scatter(y) |> 
  echarts4r::e_text_g(
    style = list(text = "right",
                 fontSize = 30,
                 fill = "red"),
    right = "10%",
    top = "7%") |> 
  echarts4r::e_text_g(
    style = list(text = "left",
                 fontSize = 30,
                 fill = "red"),
    left = "10%",
    top = "7%"
  )

echarts_both

Expected Behavior

I would expect to successfully place text on both the left and right sides of the plot by chaining the e_text_g commands.

Thank you for looking into this issue. Any alternative approaches or suggestions for achieving the placement of text on both sides of the plot with reproducible distances from the borders are greatly appreciated.

dernesa commented 5 months ago

I posted the problem also on stack overflow.

rdatasculptor commented 5 months ago

Well, I digged into the code and I think echarts4r doesn't seem to cause this unexpected behaviour. I am starting to think echarts itself doesn't allow for more than one graphical element per chart. @munoztd0, what do you think?