Remove comma in thousands in echarts4r

Viewed 329

Here's a plot made with echarts4r:

library(tibble)
library(echarts4r)

data_test <- tibble(
  year = seq(1900, 1920, 1),
  variable = seq(200, 400, 10)
)

data_test %>%
  e_charts(year) %>%
  e_x_axis(year) %>%
  e_y_axis(variable) %>%
  e_line(variable) 

How can I format the values on the x-axis so that I have 1900 instead of 1,900 for example?

I checked here but couldn't find a solution.

2 Answers

Probably not the most elegant solution, but this should work :

data_test %>%
    e_charts(year) %>%
    e_x_axis(type='category') %>%
    e_y_axis(variable) %>%
    e_line(variable)

Adding options for a cleaner output :

data_test %>%
    e_charts(year) %>%
    e_x_axis(type='category',axisLabel = list(interval = 4),axisTick = list(inside=TRUE,alignWithLabel=TRUE,interval=4)) %>%
    e_y_axis(variable) %>%
    e_line(variable)

Output :

eChart

I opened a GitHub issue for this question and this is the answer of the developer of echarts4r (I shortened it a little, see the link for the original answer).

There are two solutions.

1) Transform the variable year as a factor. This works only if there are no missing years.

library(tibble)
library(echarts4r)

data_test <- tibble(
    year = seq(1900, 1920, 1),
    variable = seq(200, 400, 10)
)

data_test %>%
    dplyr::mutate(year = as.factor(year)) %>% 
    e_charts(year) %>%
    e_line(variable)

2) Modify the JavaScript function. This is more robust, and it works when there are missing years.

library(echarts4r)

data_test <- tibble(
    year = c(1900, 1901, 1905),
    variable = 1:3
)

label <- list(
    formatter = htmlwidgets::JS(
        'function(value, index){
            return value;
        }'
    )
)

data_test %>%
    e_charts(year) %>%
    e_y_axis(variable) %>% 
    e_line(variable) %>% 
    e_x_axis(serie = year, axisLabel = label)
Related