首先,加载recharts
:
library(recharts)
适当的标注线让成图更有信息性。我们可以用addMarkLine
(或简写为addML
) 一个系列一系列、一个时间轴切片一个时间轴切片地添加标注线. 也可以用overideMarkLine
(或简写为 overideML
) 覆写已有的标注线。
所有函数名都有小驼峰法和小写连划线(Hadly Wickham推荐此法)两种形式。为了便于偷懒,还有缩写版(如markLine缩写成ml)。
addMarkLine(chart, series = NULL, timeslots = NULL, data = NULL,
clickable = TRUE, symbol = c("circle", "arrow"), symbolSize = c(2, 4),
symbolRotate = NULL, large = FALSE, smooth = FALSE, smoothness = 0.2,
precision = 2, bundling = list(enable = FALSE, maxTurningAngle = 45),
effect = list(show = FALSE), itemStyle = NULL, mode = c("add",
"overide"), ...)
addML(chart, series = NULL, timeslots = NULL, data = NULL,
clickable = TRUE, symbol = c("circle", "arrow"), symbolSize = c(2, 4),
symbolRotate = NULL, large = FALSE, smooth = FALSE, smoothness = 0.2,
precision = 2, bundling = list(enable = FALSE, maxTurningAngle = 45),
effect = list(show = FALSE), itemStyle = NULL, mode = c("add",
"overide"), ...)
addMarkline(chart, series = NULL, timeslots = NULL, data = NULL,
clickable = TRUE, symbol = c("circle", "arrow"), symbolSize = c(2, 4),
symbolRotate = NULL, large = FALSE, smooth = FALSE, smoothness = 0.2,
precision = 2, bundling = list(enable = FALSE, maxTurningAngle = 45),
effect = list(show = FALSE), itemStyle = NULL, mode = c("add",
"overide"), ...)
add_markline(chart, series = NULL, timeslots = NULL, data = NULL,
clickable = TRUE, symbol = c("circle", "arrow"), symbolSize = c(2, 4),
symbolRotate = NULL, large = FALSE, smooth = FALSE, smoothness = 0.2,
precision = 2, bundling = list(enable = FALSE, maxTurningAngle = 45),
effect = list(show = FALSE), itemStyle = NULL, mode = c("add",
"overide"), ...)
add_ml(chart, series = NULL, timeslots = NULL, data = NULL,
clickable = TRUE, symbol = c("circle", "arrow"), symbolSize = c(2, 4),
symbolRotate = NULL, large = FALSE, smooth = FALSE, smoothness = 0.2,
precision = 2, bundling = list(enable = FALSE, maxTurningAngle = 45),
effect = list(show = FALSE), itemStyle = NULL, mode = c("add",
"overide"), ...)
overideMarkLine(chart, ...)
overideMarkline(chart, ...)
overideML(chart, ...)
overideML
与addML(mode='overide', ...)
等价。
参数 | 要求 |
---|---|
chart |
|
series |
数值型(系列索引值)或文本型(系列名称),前者更好。如设为NULL,则作用于所有系列。 |
timeslots |
数值型(时间轴切片索引值)或文本型(时间轴切片名称),前者更好。如设为NULL,则作用于所有时间轴切片。可简写为z. |
data |
标注线源数据,数据框形式。必须包含下列列: name1, name2, value / type 和/或 x1 / xAxis1, y1 / yAxis1, x2 / xAxis2, y2 / yAxis2 和/或 series. 如提供series,必须与
|
clickable |
逻辑型,数据图形是否可以点击,默认为TRUE. |
symbol |
标注线图标向量,参见 |
symbolSize |
数值型向量或 |
symbolRotate |
数值型, -180 ~ 180. 默认为NULL. |
large |
数值型, 是否开启大规模效果. 默认为FALSE. |
effect |
列表,标注线效果配置器。默认为NULL, 即 |
itemStyle |
列表,结构为 |
mode |
将标注线的data部分’add’ 或 ‘overide’到echarts对象. 默认为’add’. 也可以直接用 |
… |
省略号 |
下例来自Scatterplot Manual.
lm <- with(iris, lm(Petal.Width~Sepal.Width))
pred <- predict(lm, data.frame(Sepal.Width=c(2, 4.5)))
我们分别向数据系列1 (‘setosa’), 2 (‘versicolor’) 和3 (‘virginica’)添加最大、平均、最小值标注线。然后额外添加一条线性回归线。
第四条标注线比较特别,其数据结构是一个数据框,包含’name1’, ‘value’, ‘xAxis1’, ‘yAxis1’, ’xAxis2’和 ’yAxis2’列。这是直角坐标系唯一接受的标注线数据结构。
g = echartr(iris, Sepal.Width, Petal.Width, Species) %>%
setToolbox(show=FALSE) %>% setTheme(width=400, height=300)
|
|
|
|
也可以把这些标注线数据整合成一个数据框,包含有效的数据列: ‘name1’, ‘type’, ‘series’, 等。其中,’series’必须与addML
调用参数中的series
参数相对应(即,数据框的每一行都映射到 addML
的series
向量).
data <- data.frame(
name1=c('Max', 'Mean', 'Min', 'Linear Regr'),
type=c('max', 'average', 'min', NA),
series=c(levels(iris$Species), 'Linear Regr'),
value=c(rep(NA, 3), round(lm$coefficients[2], 3)),
xAxis1=c(rep(NA, 3), 2),
yAxis1=c(rep(NA, 3), pred[1]),
xAxis2=c(rep(NA, 3), 4.5),
yAxis2=c(rep(NA, 3), pred[2])
)
knitr::kable(data)
name1 | type | series | value | xAxis1 | yAxis1 | xAxis2 | yAxis2 |
---|---|---|---|---|---|---|---|
Max | max | setosa | NA | NA | NA | NA | NA |
Mean | average | versicolor | NA | NA | NA | NA | NA |
Min | min | virginica | NA | NA | NA | NA | NA |
Linear Regr | NA | Linear Regr | -0.64 | 2 | 1.876319 | 4.5 | 0.2756276 |
echartr(iris, Sepal.Width, Petal.Width, Species) %>%
addML(series=c(levels(iris$Species), 'Linear Regr'), data=data)
对于非直角坐标系(如极坐标、地理坐标),不要提供映射到坐标轴的数据结构。对于极坐标系,x和y对应于画布坐标,而在地理坐标系里,必须用addGeoCoord
传入所涉及的坐标,而不能直接用addML
来传。
注意,画布坐标的零点是左上角。
用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(
name1=c('hor', 'ver'), name2=c('', ''), value=c(NA, NA), x1=c(10, 336),
y1=c(240, 470), x2=c(660, 336), y2=c(240, 10)
)
knitr::kable(data)
name1 | name2 | value | x1 | y1 | x2 | y2 |
---|---|---|---|---|---|---|
hor | NA | 10 | 240 | 660 | 240 | |
ver | NA | 336 | 470 | 336 | 10 |
g %>% addML(series='crossing', data=data) %>%
setSeries(series='crossing', type='line')