Welcome to echarts4r, let’s explore the package together.

  1. All functions of the echarts4r package start with e_.
  2. All its Shiny proxies end with _p.
  3. All echarts4r plots are initialised with e_charts.
  4. All functions are %>% friendly.
  5. Most functions have escape hatches ending in _.

Your first plots

Let’s build a line chart, load the library and pipe your data to e_charts. If you are not at ease with the %>% you can use e_charts(mtcars, wt).

If you are not at ease with bare column names you can use the escape hatches ending in _.

The easiest way is to use the %>% operator to add plots and options, we’ll change the minimum of our x axis.

mtcars %>% 
  e_charts(wt) %>% # initialise
  e_line(qsec) %>% # add line
  e_area(mpg) %>% # add area
  e_x_axis(min = 1) # change x axis minimum

We could also change the lines to make them smooth.

mtcars %>% 
  e_charts(wt) %>% # initialise
  e_line(qsec, smooth = TRUE) %>% # add smooth line
  e_area(mpg, smooth = TRUE) %>% # add area
  e_x_axis(min = 1) # change x axis minimum

qsec and mpg isn’t clear to reader, rename them, lets also label the x axis while we’re at it.

mtcars %>% 
  e_charts(wt) %>% # initialise
  e_line(qsec, smooth = TRUE, name = "1/4 mile time") %>% # add smooth line
  e_area(mpg, smooth = TRUE, name = "Miles/gallon") %>% # add area
  e_x_axis(min = 1, name = "weight") # change x axis minimum

We can use one of the 13 built-in themes, see ?e_theme for a complete list, we’ll also add a title with e_title.

mtcars %>% 
  e_charts(wt) %>% # initialise
  e_line(qsec, smooth = TRUE, name = "1/4 mile time") %>% # add smooth line
  e_area(mpg, smooth = TRUE, name = "Miles/gallon") %>% # add area
  e_x_axis(min = 1, name = "weight") %>%  # change x axis minimum
  e_title("Ol' mtcars example") %>%  # Add title
  e_theme("infographic") # theme

The legend and title are a bit close, let’s move the legend to another part the canvas.

mtcars %>% 
  e_charts(wt) %>% # initialise
  e_line(qsec, smooth = TRUE, name = "1/4 mile time") %>% # add smooth line
  e_area(mpg, smooth = TRUE, name = "Miles/gallon") %>% # add area
  e_x_axis(min = 1, name = "weight") %>%  # change x axis minimum
  e_title("Ol' mtcars example") %>%  # Add title
  e_theme("infographic") %>%  # theme
  e_legend(bottom = 0) # move legend to the bottom

Add a tooltip, of which there are numerous options, here we use trigger = "axis" to trigger the tooltip by the axis rather than a single data point.

mtcars %>% 
  e_charts(wt) %>% # initialise
  e_line(qsec, smooth = TRUE, name = "1/4 mile time") %>% # add smooth line
  e_area(mpg, smooth = TRUE, name = "Miles/gallon") %>% # add area
  e_x_axis(min = 1, name = "weight") %>%  # change x axis minimum
  e_title("Ol' mtcars example") %>%  # Add title
  e_theme("infographic") %>% # theme
  e_legend(bottom = 0) %>% # move legend to the bottom
  e_tooltip(trigger = "axis") # tooltip

You can also customise the axis with e_axis or its sister functions, e_x_axis and e_y_axis. There are also helper functions like e_format_axis to easily add suffixes or prefixes to your axis labels.

mtcars %>% 
  e_charts(wt) %>% # initialise
  e_line(qsec, smooth = TRUE, name = "1/4 mile time") %>% # add smooth line
  e_area(mpg, smooth = TRUE, name = "Miles/gallon") %>% # add area
  e_x_axis(min = 1, name = "weight") %>%  # change x axis minimum
  e_title("Ol' mtcars example") %>%  # Add title
  e_theme("infographic") %>% # theme
  e_legend(bottom = 0) %>%  # move legend to the bottom
  e_tooltip(trigger = "axis") %>%  # tooltip
  e_format_y_axis(suffix = "mpg")

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.

iris %>% 
  group_by(Species) %>% 
  e_charts(Sepal.Length) %>% 
  e_line(Sepal.Width) %>% 
  e_title("Grouped data")

You’re in on the basics, go to the advanced section or navigate the site to discover how add multiple linked graphs, draw on globes, use the package in shiny, and more.