Search code examples
recharts4r

How to return different values ​of min, avg and max in e_mark_point?


I have this data:

library(echarts4r)

x <- c(10, 20, 30, 45, 46, 50)
y <- c(12, 25, 36, 46, 49, 59)

matrixinc1 <- data.frame(x, y)

near_line <- function(matriz) {
  diferenca <- abs(matriz[, 1] - matriz[, 2])
  indice <- which.min(diferenca)
  return(matriz[indice, , drop = FALSE])
}

near <- near_line(matrixinc1)
near

matrixinc1 |>
  e_charts(x) |>
  e_line(x) |>
  e_line(y) |>
  e_mark_point(data = near) 

I tried to make the x value represented by 45 and the y value be represented by 46. But I couldn't make this adjustment in the graph. Is it possible to do this so that the markers are not on top of each other?

I tried this too:

matrixinc1 |>
  e_charts(x) |>
  e_line(x) |>
  e_line(y) |>
  e_mark_point("x", data = near[,1]) |> 
  e_mark_point("y", data = near[,2]) 
 

And this:

 matrixinc1[["test"]] <- near$x

avg <- list(
  type = "value",
  value = near$x,  # Atribui o valor de near[,1] à propriedade 'value'
  name = "AVG"
)

matrixinc1 |>
  e_charts(x) |>
  e_line(x) |>
  e_line(y) |>
  e_mark_point("x", data = avg)
 

Solution

  • From your question it's not clear what you are trying to achieve. If you want to place markers at specific points you could do so by setting the coord attribute which should be a vector containing the x and the y coordinate. To this end I refactored you function to return the required vector.

    library(echarts4r)
    
    x <- c(10, 20, 30, 45, 46, 50)
    y <- c(12, 25, 36, 46, 49, 59)
    
    matrixinc1 <- data.frame(x, y)
    
    near_line <- function(matriz, cols = c("x", "y")) {
      diferenca <- abs(matriz[, 1] - matriz[, 2])
      indice <- which.min(diferenca)
    
      unname(
        unlist(
          matriz[indice, cols, drop = TRUE]
        )
      )
    }
    
    near_y <- list(
      coord = near_line(matrixinc1),
      value = "y"
    )
    
    near_x <- list(
      coord = near_line(matrixinc1, c("x", "x")),
      value = "x"
    )
    
    matrixinc1 |>
      e_charts(x) |>
      e_line(x) |>
      e_line(y) |>
      e_mark_point(serie = "y", data = near_y) |>
      e_mark_point(
        serie = "x", data = near_x, symbolRotate = 180,
        label = list(verticalAlign = "top")
      )
    

    enter image description here