This document details some more advanced usage of the
You can set two common options with
All charts on this page will use the
Ubuntu Mono font (sourced in CSS).
The package also understands
dplyr::group_by in order to avoid having to add many layers manually one by one.
echarts4r essentially will plot one serie for each group.
The above plot one line (
e_line) for each group (
group_by). At one exception, since
timeline is set to
TRUE when the chart is initialised with
e_charts. In the latter case each group is used as a step of the timeline.
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
numeric and return a similar vector of the same length. For instance, to create a function that rescales from 5 to 30:
size is not passed the size of the poitns are defined by
size is passed
symbol_size becomes a multiplier executed after the scaling function is run. For instance \(\log(1+x)*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.
If you want to keep the original size then you can use
scale_js, because you might want the
e_visual_map to remain true to the original data.
This way the bubbles are of an appropriate size but the the range of the visual map remains true.
0.2.1 you can jitter points with
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.
v <- LETTERS[1:10] matrix <- data.frame( x = sample(v, 300, replace = TRUE), y = sample(v, 300, replace = TRUE), z = rnorm(300, 10, 1), stringsAsFactors = FALSE ) %>% dplyr::group_by(x, y) %>% dplyr::summarise(z = sum(z)) %>% dplyr::ungroup() matrix %>% e_charts(x) %>% e_heatmap(y, z) %>% e_visual_map(z)
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.
There are numerous coordinate system available in
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
y_index = 1 is the second axis,
y_index = 0 is the first axis (default)
You can flip coordinates with
e_flip_coords, note that it will not work if you have multiple y axis.
Highlight points and lines on your plot with the
e_mark family of functions.
Look for more arguments, with
echarts4r you are often only one argument away from from what you want.
You can, for most series show labels like so:
There is also a helper function that provides an easier API
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,
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
# add columns to iris iris_dat <- iris %>% dplyr::mutate( show = TRUE, # to show the labels fontSize = exp(Sepal.Length) / 10, # font size will correspond to Sepal.Length color = sample(c("red", "black", "blue"), n(), replace = TRUE) # assign a random color to the label ) iris_dat %>% dplyr::slice(1:10) %>% # simplify the graph. e_charts(Sepal.Width) %>% e_line(Sepal.Length) %>% e_add("label", show, fontSize, color) %>% # add our columns to "label" e_x_axis(min = 2.5)
Another example with
0.2.1 you can pass a raw list of options.
e_list can also be used to append options.