R echarts4r - 在轴中格式化日期

R echarts4r - format dates in axis

echarts4r中如何控制日期格式?有货币、百分比和小数的格式化程序,但我看不到日期。

这是来自 https://rpubs.com/paul_simmering/echarts

的示例
library(echarts4r)
library(nycflights13) # data
library(tidyverse)

flights_ts <- flights %>% 
  transmute(week = as.Date(cut(time_hour, "week")), dep_delay, origin) %>% 
  group_by(origin, week) %>% # works with echarts
  summarise(dep_delay = sum(dep_delay, na.rm = TRUE))

ts_base <- flights_ts %>% 
  e_charts(x = week) %>% 
  e_datazoom(
    type = "slider", 
    toolbox = FALSE,
    bottom = -5
  ) %>% 
  e_tooltip() %>% 
  e_title("Departure delays by airport") %>% 
  e_x_axis(week, axisPointer = list(show = TRUE))

ts_base %>% e_line(dep_delay)

图表日期的格式为月-日-年,我想将其更改为年-月-日:

您可以将自定义 JavaScript 函数传递给 axisLabel 中的 formatter 参数,如下所示:

library(echarts4r)
library(nycflights13) # data
library(tidyverse)

yearMonthDate <- htmlwidgets::JS('function (value) {
  var d = new Date(value);
  var datestring = d.getFullYear() + "-" + ("0"+(d.getMonth()+1)).slice(-2) + "-" + ("0" + d.getDate()).slice(-2)
  return datestring
}')

ts_base <- flights_ts %>% 
  e_charts(x = week) %>% 
  e_datazoom(
    type = "slider", 
    toolbox = FALSE,
    bottom = -5
  ) %>% 
  e_tooltip() %>% 
  e_title("Departure delays by airport") %>% 
  e_x_axis(
    week, 
    axisPointer = list(show = TRUE),
    axisLabel = list(
      formatter = yearMonthDate
    ))

ts_base %>% e_line(dep_delay)

您将获得: