First, you should load recharts
:
library(recharts)
Proper markpoints makes a chart more informative. We can use addMarkPoint
(or addMP
for short) to add markPoints series by series, timeline by timeline. We can also use overideMarkPoint
(or overideMP
for short) to overide existing markPoints.
All the function names have two forms: small camel and lowercase_underscore (endorsed by Hadley Wickham). To be lazy, we also provide short forms as well (e.g., mp for markPoint).
addMarkPoint(chart, series = NULL, timeslots = NULL, data = NULL,
clickable = TRUE, symbol = "pin", symbolSize = 10,
symbolRotate = NULL, large = FALSE, effect = list(show = FALSE),
itemStyle = NULL, ...)
addMP(chart, series = NULL, timeslots = NULL, data = NULL,
clickable = TRUE, symbol = "pin", symbolSize = 10,
symbolRotate = NULL, large = FALSE, effect = list(show = FALSE),
itemStyle = NULL, ...)
add_markpoint(chart, series = NULL, timeslots = NULL, data = NULL,
clickable = TRUE, symbol = "pin", symbolSize = 10,
symbolRotate = NULL, large = FALSE, effect = list(show = FALSE),
itemStyle = NULL, ...)
add_mp(chart, series = NULL, timeslots = NULL, data = NULL,
clickable = TRUE, symbol = "pin", symbolSize = 10,
symbolRotate = NULL, large = FALSE, effect = list(show = FALSE),
itemStyle = NULL, ...)
addMarkpoint(chart, series = NULL, timeslots = NULL, data = NULL,
clickable = TRUE, symbol = "pin", symbolSize = 10,
symbolRotate = NULL, large = FALSE, effect = list(show = FALSE),
itemStyle = NULL, ...)
overideMarkPoint(chart, ...)
overideMarkpoint(chart, ...)
overideMP(chart, ...)
overide_mp(chart, ...)
overide_markpoint(chart, ...)
overideMP
is exactly the same as addMP(mode='overide', ...)
.
Arg | Requirement |
---|---|
chart |
Echarts object generated by |
series |
Numeric (series index) or character (series name), numeric preferred. If set NULL, then apply the markPoint to all the series. |
timeslots |
Numeric (timeslot index) or character (timeslot name), numeric is preferred. If set NULL, then apply the markPoint to all the timeslots. You can use z for short. |
data |
Data.frame, the data of the markPoints. It must contain the following columns:
Note that markLine dataset is compatible in
|
clickable |
Logical, if the points are clickable. Default TRUE. |
symbol |
Symbol of the markpoints, refer to recharts:::validSymbols. Default ‘pin’. |
symbolSize |
Numeric or vector |
symbolRotate |
Numeric -180 ~ 180. Default NULL. |
large |
Logical, if large effect is on. Default FALSE. |
effect |
List. effect configurator of markPoints. Default NULL, which is |
itemStyle |
List. It is a list with the structure |
mode |
‘add’ or ‘overide’ the data part of markPoint to the echarts object. Default ‘add’. You can use |
… |
Elipsis. |
The example below is from Scatterplot Manual.
We, respectively, add markPoints of max, min of data series 1 (‘setosa’) and 2 (‘versicolor’). And we add another two markPoints with absolute position.
The thrid markPoint calls a special data strucutre: a data.frame containing ‘name’, ‘value’, ‘xAxis’, ‘yAxis’. This is the only acceptable form for Cartesian coordinate system.
The fourth markPoint calls a generic data structure which refer to absolute coodinates on the canvas. However, it cannot be correctly parsed in Cartesian coordinate system, and hence is place at the zero coordinate point.
g = echartr(iris, Sepal.Width, Petal.Width, Species) %>%
setToolbox(show=FALSE) %>% setTheme(width=400, height=300)
|
|
|
|
We can combine the data for the three markPoints into a data.frame with valid columns names: ‘name’, ‘type’, ‘series’, etc., of which ‘series’ must be corrsponding to the series
argumnet (i.e. each row of data will be mapped to series
argument of addMP
).
data <- data.frame(
name=c('Max', 'Min', 'Misc', 'Not Known'), type=c('max', 'min', NA, NA),
series=c(levels(iris$Species), 'Not Known'), x=c(NA, NA, NA, 350),
y=c(NA, NA, NA, 300), xAxis=c(NA, NA, 3, NA), yAxis=c(NA, NA, 5, NA))
knitr::kable(data)
name | type | series | x | y | xAxis | yAxis |
---|---|---|---|---|---|---|
Max | max | setosa | NA | NA | NA | NA |
Min | min | versicolor | NA | NA | NA | NA |
Misc | NA | virginica | NA | NA | 3 | 5 |
Not Known | NA | Not Known | 350 | 300 | NA | NA |
echartr(iris, Sepal.Width, Petal.Width, Species) %>%
addMP(series=c(levels(iris$Species), 'Not Known'), data=data)
When it applies to non-cartesian coordinate system (polar or geographic), do not provide an axis-mapping form of dataset. For polar coordinate system, x and y are cooresponding to position on the canvas, while for geographic system, it accepts lng and lat from addGeoCoord
instead of addMP
call.
Note that the zero point on the canvas is upper-left point.
Let’s use the example in Pie Chart Manual.
titanic <- data.table::melt(apply(Titanic, c(1,4), sum))
names(titanic) <- c('Class', 'Survived', 'Count')
g <- echartr(titanic, Survived, Count, facet=Class, type='pie')
## Warning in split_indices(.group, .n): '.Random.seed' is not an integer
## vector but of type 'NULL', so ignored
data <- data.frame(
name=rep('', 4), value=1:4, x=c(190, 490, 190, 490), y=c(132, 132, 348, 348)
)
knitr::kable(data)
name | value | x | y |
---|---|---|---|
1 | 190 | 132 | |
2 | 490 | 132 | |
3 | 190 | 348 | |
4 | 490 | 348 |
g %>% addMP(series='Series Index', data=data) %>%
setSeries(series='Series Index', type='scatter')