Mathias
Mathias

Reputation: 43

Echarts4r: Difficulty chaining e_text_g for dual left and right placement

Encountered a challenge while attempting to utilize e_text_g() in echarts4r to place text on both the left and right sides of a plot. While applying e_text_g() individually for left and right sides works seamlessly, combining both commands in a chain results in failure. A reproducible example is provided below:

library(echarts4r)

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

Text placement 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%"
  )

enter image description here

Similarly, 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%"
  )

enter image description here

However, when trying to place text on both sides by chaining both commands, this does not work:

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

enter image description here

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

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.

Upvotes: 0

Views: 81

Answers (1)

Mathias
Mathias

Reputation: 43

This is a bit of a hack, but this works as expected:

Using e_title() + e_text_g()

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

enter image description here

Using 2x e_title()

df |>
  e_charts(x) |>
  e_scatter(y) |>
  echarts4r::e_title(
    text = "right",
    textStyle = list(
      fontSize = 30,
      color = "red",
      fontWeight = 'normal'
    ),
    right = "10%",
    top = "7%"
  ) |>
  echarts4r::e_title(
    text = "left",
    textStyle = list(
      fontSize = 30,
      color = "red",
      fontWeight = 'normal'
    ),
    left = "10%",
    top = "7%"
  ) 

enter image description here

Upvotes: 1

Related Questions