This document details some more advanced usage of the echarts4r package.

Scatter

Since 0.2.0 the e_scatter and e_effect_scatter functions take a different, much improved scale argument which is a scaling function.

The default scaling function is e_scale which rescales from 1 to 20, but you can pass your own. The scaling function should take a vector of integer or numeric and return a similar vector of the same length. For instance, to create a function that rescales from 5 to 30:

my_scale <- function(x){
  scales::rescale(x, to = c(5, 30))
}

iris %>% 
    group_by(Species) %>% 
    e_charts(Sepal.Length) %>% 
    e_scatter(Petal.Length, Sepal.Width, scale = my_scale)

When size is not passed the size of the poitns are defined by symbol_size.

iris %>% 
    group_by(Species) %>% 
    e_charts(Sepal.Length) %>% 
    e_scatter(Petal.Length, symbol_size = 15)

When size is passed symbol_size becomes a multiplier executed after the scaling function is run. For instance \(\log(1+x)*5\)

iris %>% 
    group_by(Species) %>% 
    e_charts(Sepal.Length) %>% 
    e_scatter(Petal.Length, Sepal.Width, scale = log1p, symbol_size = 5)

This behaviour was added as it is important to be able to scale points within a reasonable range, ideally points are between 1 and 30~40 in size.

Note that rescaling the color means you should also rescale the e_visual_map, if used.

echart <- mtcars %>% 
  e_charts(mpg) %>% 
  e_scatter(qsec, wt, scale = e_scale) %>% 
  e_legend(show = FALSE)

echart %>% 
  e_visual_map(wt, scale = e_scale)

coord_system

Chart types are not only applicable to the standard 2D cartesian coordinate system, though most charts will default to the cartesian2d coordinate system, they may be applied to others.

Let’s look at the heatmap. First a regular heatmap.

One could also plot the heatmap on different coordinates, such as a calendar by first adding a calendar with e_calendar then specifying coord_system = "calendar".

Another example, using polar coordinates, plot a line on 2D cartesian coordinates, then change to polar.

df %>% 
  e_charts(x) %>% 
  e_polar() %>% 
  e_angle_axis() %>% 
  e_radius_axis() %>% 
  e_line(y, coord_system = "polar", smooth = TRUE) 

There are numerous coordinate system available in echarts4r; globe, cartesian3d and polar to name a few. Note that when there are more than one coord_system available the latter are documented with examples in their respective function’s man page.

Customise the Axis

Use multiple axis, by speecifying the index of the axis. Note that JavaScript starts from 0 not 1, so y_index = 1 is the second axis, y_index = 0 is the first axis (default)

USArrests %>% 
  e_charts(Assault) %>% 
  e_line(Murder, smooth = TRUE) %>% 
  e_line(Rape, y_index = 1) %>%  # add secondary axis
  e_y_axis(spliLine = list(show = FALSE)) # hide split lines on first Y axis

Flip coordinates

You can flip coordinates with e_flip_coords, note that it will not work if you have multiple y axis.

Mark Points and Lines

Highlight points and lines on your plot with the e_mark family of functions.

USArrests %>% 
  dplyr::mutate(
    State = row.names(.),
    Rape = -Rape
  ) %>% 
  e_charts(State) %>% 
  e_area(Murder) %>%
  e_bar(Rape, name = "Sick basterd", x_index = 1) %>% # second y axis 
  e_mark_line("Sick basterd", data = list(type = "average")) %>% 
  e_mark_point("Murder", data = list(type = "min"))

Look for arguments

Look for more arguments, with echarts4r you are often only one argument away from from what you want.

to radial:

Show labels

You can, for most series show labels like so:

USArrests %>% 
    dplyr::mutate(
        State = row.names(.)
    ) %>% 
    dplyr::slice(1:10) %>% 
    e_charts(State) %>% 
    e_area(Murder, label = list(normal = list(show = TRUE))) 

There is also a helper function that provides an easier API

Nested data

You might observe in the official documentation that some series can take more data points than just x and y points, like e_bar; In the official documentation go to “serie”, select the one you are interested in (i.e.: bar) then select “data”.

e_bar lets you pass serie (from your initial data.frame) which corresponds to value in the original library. However the latter also takes, label, itemStyle, and tooltip but being JSON arrays they translate to lists in R and dealing with nested data.frames is not ideal. e_add remedies to that. It allows adding those nested data points.

Let’s add some columns to the iris dataset which we will use in e_add to customize the appearance of the label.

Another example with e_funnel.

List

Since version 0.2.1 you can pass a raw list of options.

e_list can also be used to append options.