get_started.RmdWelcome to echarts4r, let’s explore the package together.
echarts4r package start with e_._p.echarts4r plots are initialised with e_charts.%>% friendly._.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).
# prepare data
df <- state.x77 %>%
as.data.frame() %>%
tibble::rownames_to_column("State")library(echarts4r) # load echarts4r
df %>%
e_charts(x = State) %>% # initialise and set x
e_line(serie = Population) # add a lineIf 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 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 areaLets label the axes with the convenience function e_axis_labels.
df %>%
e_charts(State) %>% # initialise and set x
e_line(Population, smooth = TRUE) %>% # add a line
e_area(Income, smooth = TRUE) %>% # add area
e_axis_labels(x = "State") # axis labelsWe 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_axis_labels(x = "State") %>% # axis labels
e_title("US States", "Population & Income") %>% # Add title & subtitle
e_theme("infographic") # themeThe 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_axis_labels(x = "State") %>% # axis labels
e_title("US States", "Population & Income") %>% # Add title & subtitle
e_theme("infographic") %>% # theme
e_legend(right = 0) # move legend to the bottomAdd 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_axis_labels(x = "State") %>% # axis labels
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") # tooltipFinally, 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.
Dual y axes is a terrible idea, it’s only here for demonstration purposes.
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_axis_labels(x = "State") %>% # axis labels
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