First, you should load recharts:

library(recharts)

1 Introduction

The default position of legend is 11 o’clock.

The recommended approach to use setLegend is chart %>% setTitle(...).

2 Function Call

All the function names have two forms: small camel and lowercase_underscore (endorsed by Hadley Wickham).

setLegend(chart, show = TRUE, pos = 11, selected = NULL, itemGap = 5,
  borderColor = "#ccc", borderWidth = 0, textStyle = list(color = "auto"),
  formatter = NULL, overideData = NULL, ...)

set_legend(chart, show = TRUE, pos = 11, selected = NULL, itemGap = 5,
  borderColor = "#ccc", borderWidth = 0, textStyle = list(color = "auto"),
  formatter = NULL, overideData = NULL, ...)
Arg Requirement

chart

Echarts object generated by echartR or echart.

show

Logical. Show the legend or not. Default TRUE. If you want to remove legend from the echarts object, set it NULL.

pos

Clock position of the legend. Default 11. Refer to vecPos. Or you can define the position vector c(x, y, orient) yourself.

selected

A vector of series names that are selected on load.

  • one series name: single series is selected on load and the selectedMode is ‘single’
  • muti series name: multiple series are selected on load and the selectedMode is ‘multiple’
  • ‘none’: no series is selected on load

itemGap

The gap between legend items. Default 5px.

borderColor

The border color of the legend. Default ‘#ccc’.

borderWidth

The border width of the legend. Default 0px (not shown).

textStyle

A list of textStyle definition to decorate the text. E.g., list(color='#444') or list(color='auto').

formatter

A named formatter template or a string containing javascript codes. E.g., '{name}'.

overideData

A list of data to overide the legend text. E.g., list(list(name='Series 1', icon='image://../asset/ico/favicon.png', textStyle=list(color='#bbb')))

Elipsis.

3 Showcase

g1 <- echartr(iris, Sepal.Width, Petal.Width, Species)
g2 <- echartr(iris, Sepal.Width, Petal.Width, t=Species)
g1 %>% setTheme(width=400, height=300)
g2 %>% setTheme(width=400, height=300)

Initially legend is shown. Let’s turn if off.

g1 %>% setLegend(show=FALSE)

Relocate the legend.

g1 %>% setLegend(pos=12)

Select series 2 (‘versicolor’) on load. selectedMode is ‘single’.

g1 %>% setLegend(selected='versicolor')

Select nothing on load.

g1 %>% setLegend(selected='none')

Customize the format.

g1 %>% setLegend(textStyle=textStyle(
    fontFamily='Times New Roman', color='purple', fontWeight='bold', 
    fontSize=16))

Customize the format more specifically.

g1 %>% setLegend(overideData=list(
    list(name='setosa', textStyle=textStyle(color='red')),
    list(name='versicolor', textStyle=textStyle(color='green')),
    list(name='virginica', textStyle=textStyle(color='blue'))))