echarts4r icon indicating copy to clipboard operation
echarts4r copied to clipboard

Fliping axis using e_flip_coords() while using e_visual_map() gets the coloring wrong

Open pelishk opened this issue 4 years ago • 4 comments

Hello there

I have the following issue, when I do this everything works ok

   r_comparativo %>%
    e_charts(seccion) %>%
    e_bar(cambio)  %>%
    e_visual_map(
      type = "piecewise",
      pieces = list(
        list(gt = 0, color = "green"),
        list( lte = 0, color = "red"
        ) ) )

but if I try to flip the axis it breaks and doesn't assign colors correctly

   r_comparativo %>%
    e_charts(seccion) %>%
    e_bar(cambio)  %>%
    e_visual_map(
      type = "piecewise",
      pieces = list(
        list(gt = 0, color = "green"),
        list( lte = 0, color = "red"
        ) ) ) %>%
    e_flip_coords()

Regards Pablo

pelishk avatar Apr 14 '20 19:04 pelishk

It's difficult to tell without the data. It looks like it's behaving correctly to me.

df <- tibble::tibble(
  x = 1:10,
  y = runif(10, -10, 10)
)

library(echarts4r)

df %>%
  e_charts(x) %>%
  e_bar(y)  %>%
  e_visual_map(
    type = "piecewise",
    pieces = list(
      list(gt = 0, color = "green"),
      list( lte = 0, color = "red"
      ) ) )

OR

library(echarts4r)

df %>%
  e_charts(y) %>% # inverse then flip
  e_bar(x)  %>%
  e_visual_map(
    type = "piecewise",
    pieces = list(
      list(gt = 0, color = "green"),
      list( lte = 0, color = "red"
      ) ) ) %>%
  e_flip_coords()

Could you share the data, or a sample of it, as well as the effect you are trying to produce?

JohnCoene avatar Apr 14 '20 21:04 JohnCoene

Of course, here is the exact table I'm using for the plot

data.zip

pelishk avatar Apr 14 '20 21:04 pelishk

Thank you, what is the output you desire to produce exactly?

JohnCoene avatar Apr 16 '20 12:04 JohnCoene

I believe this can be solved using the visual map

set.seed(250)
df <- tibble::tibble(
  x = 1:5,
  y = runif(5, -5, 5)
)

We can first plot the chart normally:

df %>%
  e_charts(x) %>%
  e_bar(y)  %>%
  e_visual_map(
    max = 5, min = -5,
    inRange = list(color = c("red","blue"))
  )

For which we get this output (correctly using the visual map so that the negative values are the most "red"). Please excuse the poor quality of the visuals!

image

If we flip coordinates, however the visual map still uses the y-axis values, not the x-axis values.

df %>%
  e_charts(x) %>%
  e_bar(y)  %>%
  e_visual_map(
    max = 5, min = -5,
    inRange = list(color = c("red","blue"))
  ) %>%
  e_flip_coords()

image

I believe changing the dimension solves this inspired by this request . Note that the answer to the question points to a wrong URL, This URL is the correct point: https://echarts.apache.org/en/option.html#visualMap-continuous.dimension.

Setting dimension = 0 will use the x-axis (which is the original y-axis before the flip), and that should solve this issue.

df %>%
  e_charts(x) %>%
  e_bar(y)  %>%
  e_visual_map(
    max = 5, min = -5,
    inRange = list(color = c("red","blue")),
    dimension = 0
  ) %>%
  e_flip_coords()

image

In terms of the actual implementation of e_flip_coords(), I do not believe that anything needs to be done, although we could potentially document this somewhere.

nschwamm avatar Dec 29 '20 17:12 nschwamm