Introduction
echarts4r version 0.2.1 supports the
timeline component!
Let’s create some phoney data to demonstrate.
# fake data
df <- data.frame(
year = c(
rep(2016, 25),
rep(2017, 25),
rep(2018, 25),
rep(2019, 25)
),
x = rnorm(100),
y = rnorm(100),
grp = c(
rep("A", 50),
rep("B", 50)
)
) While on a “standard” chart you group data to distinguish between
categories, to take advantage of the timeline component, you group them
by time steps then set timeline to
TRUEwhen initialise the chart.
By changing a single argument (timeline) you go
from:
To an acutal timeline:
Supported types
It works with the vast majority chart types, they are listed below
and in the man page of e_charts.
e_bare_linee_stepe_areae_scattere_effect_scattere_candlee_heatmape_piee_line_3de_lines_3de_bar_3de_linese_scatter_3de_scatter_gle_histograme_lme_loesse_glme_densitye_pictoriale_boxplote_mape_map_3de_line_3de_gauge
All options, i.e.: e_tooltip or e_globe,
work with the timeline component.
General Options
You can pass options to the timeline with
e_timeline_opts.
library(dplyr)
library(echarts4r.maps)
df <- USArrests
# scale 0 to 1
.scl <- function(x){
(x - min(x)) / (max(x) - min(x))
}
df |>
tibble::rownames_to_column("State") |>
mutate(
Rape = .scl(Rape),
Murder = .scl(Murder),
Assault = .scl(Assault)
) |>
select(State, Rape, Murder, Assault) |>
group_by(State) |>
tidyr::gather("Key", "Value", Murder, Rape, Assault) |>
group_by(Key) |>
e_charts(State, timeline = TRUE) |>
em_map("USA") |>
e_map(Value, map = "USA") |>
e_visual_map(min = 0, max = 1) |>
e_timeline_opts(autoPlay = TRUE) |>
e_timeline_serie(
title = list(
list(text = "Assault", subtext = "Percentage based on arrests"),
list(text = "Murder", subtext = "Percentage based on arrests"),
list(text = "Rape", subtext = "Percentage based on arrests")
)
)Time step options
The function e_timeline_opts is used to set general
options on the timeline, i.e.: auto-play like above. The full list of
options is on the official
website.
However, we can also set options specific to each timestep with
e_timeline_serie. The arguments to this function differ
quite a bit from the rest of the package. As we have to assign options
to multiple timesteps at once we need to pass vectors or lists of
options, the length of the timesteps.
library(quantmod)
getSymbols(c("GS", "GOOG")) # Goldman Sachs & Google
#> [1] "GS" "GOOG"
GS <- as.data.frame(GS)
GOOG <- as.data.frame(GOOG)
colnames <- c("Open", "High", "Low", "Close", "Volume", "Adjusted")
names(GS) <- colnames
names(GOOG) <- colnames
GS$sym <- "GS"
GOOG$sym <- "GOOG"
data <- bind_rows(GS, GOOG)
data$date <- rep(row.names(GS), 2)
data |>
group_by(sym) |>
e_charts(date, timeline = TRUE) |>
e_candle(Open, Close, Low, High, legend = FALSE) |>
e_y_axis(max = 1500) |>
e_tooltip(trigger = "axis") |>
e_timeline_opts(
axis_type = "category",
playInterval = 1500,
top = 5,
right = 50,
left = 200
) |>
e_datazoom() |>
e_timeline_serie(
title = list(
list(text = "Goldman Sachs"),
list(text = "Google")
)
) 