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 plot

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).

library(echarts4r) # load echarts4r

df %>% 
  e_charts(x = State) %>% # initialise and set x
  e_line(serie = Population) # add a line

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

df %>% 
  e_charts_("State") %>% 
  e_line_("Population")

The easiest way is to use the %>% operator to add plots and options.

df %>% 
  e_charts(State) %>% # initialise and set x
  e_line(Population) %>%  # add a line
  e_area(Income) # add area

Options

We could also change the lines to make them smooth.

df %>% 
  e_charts(State) %>% # initialise and set x
  e_line(Population, smooth = TRUE) %>%  # add a line
  e_area(Income, smooth = TRUE) # add area

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

df %>% 
  e_charts(State) %>% # initialise and set x
  e_line(Population, smooth = TRUE) %>%  # add a line
  e_area(Income, smooth = TRUE) %>%  # add area
  e_x_axis(name = "States") # add x axis name

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.

df %>% 
  e_charts(State) %>% # initialise and set x
  e_line(Population, smooth = TRUE) %>%  # add a line
  e_area(Income, smooth = TRUE) %>%  # add area
  e_x_axis(name = "States") %>%  # add x axis name
  e_title("US States", "Population & Income") %>%  # Add title & subtitle
  e_theme("infographic") # theme

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

df %>% 
  e_charts(State) %>% # initialise and set x
  e_line(Population, smooth = TRUE) %>%  # add a line
  e_area(Income, smooth = TRUE) %>%  # add area
  e_x_axis(name = "States") %>%  # add x axis name
  e_title("US States", "Population & Income") %>%  # Add title & subtitle
  e_theme("infographic") %>%  # theme
  e_legend(right = 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.

df %>% 
  e_charts(State) %>% # initialise and set x
  e_line(Population, smooth = TRUE) %>%  # add a line
  e_area(Income, smooth = TRUE) %>%  # add area
  e_x_axis(name = "States") %>%  # add x axis name
  e_title("US States", "Population & Income") %>%  # Add title & subtitle
  e_theme("infographic") %>%  # theme
  e_legend(right = 0) %>%  # move legend to the bottom
  e_tooltip(trigger = "axis") # tooltip

Finally, we are currently plotting population and income on the same axis, let’s put them each on their respective y axis by specifying an extra axis for Income.

df %>% 
  e_charts(State) %>% # initialise and set x
  e_line(Population, smooth = TRUE) %>%  # add a line
  e_area(Income, smooth = TRUE, y_index = 1) %>%  # add area
  e_x_axis(name = "States") %>%  # add x axis name
  e_title("US States", "Population & Income") %>%  # Add title & subtitle
  e_theme("infographic") %>%  # theme
  e_legend(right = 0) %>%  # move legend to the bottom
  e_tooltip(trigger = "axis") # tooltip