gpt4 book ai didi

R echarts4r - 在轴中格式化日期

转载 作者:行者123 更新时间:2023-12-05 04:58:44 24 4
gpt4 key购买 nike

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)

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

最佳答案

您可以将自定义 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)

你会得到:

enter image description here

关于R echarts4r - 在轴中格式化日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63838914/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com