First, you should load recharts
:
library(recharts)
The default position of legend is 11 o’clock.
The recommended approach to use setLegend
is chart %>% setTitle(...)
.
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 |
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 |
selected |
A vector of series names that are 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., |
formatter |
A named formatter template or a string containing javascript codes. E.g., |
overideData |
A list of data to overide the legend text. E.g., |
… |
Elipsis. |
g1 <- echartr(iris, Sepal.Width, Petal.Width, Species)
g2 <- echartr(iris, Sepal.Width, Petal.Width, t=Species)
|
|
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'))))