echarts4r:使用R和ECharts创建交互式图表(一)

echarts4r:使用R和ECharts创建交互式图表(一)

在此前的推文使用ggplot2和echarts4r绘制我的微信好友分布图我第一次使用了echarts4r包绘制图表,觉得非常棒,打算学习一下,奈何这一个月来一直奔波没时间学习,终于今天开始学习了。因为该包功能强大,所以内容很多,因此立即学完会很劳累,所以这篇推文仅仅是该包学习笔记的第一篇。此外让人非常头疼的是作者的文档网站打开速度贼慢(丁文亮的口头禅)。

安装

安装全部:

1
remotes::install_github("JohnCoene/echarts4r.suite")

一个示例:双图联动

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
library(dplyr)
library(echarts4r)
library(echarts4r.maps)
library(echarts4r.assets)

e1 <- iris %>%
group_by(Species) %>%
e_charts(
Sepal.Length,
elementId = "chart"
) %>%
e_scatter(Petal.Length, Sepal.Length) %>%
e_datazoom(show = F, y_index = 0) %>%
e_x_axis(min = 4) %>%
e_highlight(
series_name = list("setosa", "versicolor", "virginica"),
btn = "high"
) %>%
e_downplay(
series_name = list("setosa", "versicolor", "virginica"),
btn = "down"
) %>%
e_button(
"high", "Highlight",
class = "btn btn-danger",
position = "bottom"
) %>%
e_button(
"down", "Downplay",
class = "btn btn-primary",
position = "bottom"
) %>%
e_legend(F) %>%
e_title("Connect charts") %>%
e_tooltip(
trigger = "item",
axisPointer = list(
type = "cross"
)
)

e2 <- iris %>%
group_by(Species) %>%
e_charts(Petal.Length) %>%
e_line(Sepal.Length) %>%
e_connect("chart") %>%
e_datazoom(y_index = 0) %>%
e_x_axis(min = 0) %>%
e_legend(bottom = 5) %>%
e_tooltip(
trigger = "item",
axisPointer = list(
type = "cross"
)
)

e_arrange(e1, e2, rows = 1, cols = 2)

主题

默认

1
2
3
4
5
6
7
8
9
10
11
p <- USArrests %>%
dplyr::mutate(
State = row.names(.),
Rape = -Rape
) %>%
e_charts(State) %>%
e_line(Murder) %>%
e_area(Rape, name = "Sick basterd", x_index = 1) %>% # 使用第二个x轴(x_index = 1)
e_title("Your plot", "Subtext", sublink = "https://john-coene.com") %>%
e_x_axis(1, show = F) # 隐藏第二个x轴
p

dark

1
p %>% e_theme("dark")

vintage

1
p %>% e_theme("vintage")

essos

1
p %>% e_theme("essos")

wonderland

1
p %>% e_theme("wonderland")

walden

1
p %>% e_theme("walden")

chalk

1
p %>% e_theme("chalk")

infographic

1
p %>% e_theme("infographic")

macarons

1
p %>% e_theme("macarons")

roma

1
p %>% e_theme("roma")

shine

1
p %>% e_theme("shine")

purple-passion

1
p %>% e_theme("purple-passion")

halloween

1
p %>% e_theme("halloween")

custom

1
p %>% e_theme_custom('{"color": ["#ff715e", "#ffaf51"]}')

入门

1
2
3
mtcars %>%
e_charts(x = wt) %>% # 初始化和设置x轴
e_line(serie = mpg) # 添加线图

如果你对于直接使用列名感觉不舒服,可以使用以下划线结尾的函数:

1
2
3
mtcars %>%
e_charts_("wt") %>%
e_line_("mpg")

添加图层和选项:

1
2
3
4
5
mtcars %>%
e_charts(wt) %>%
e_line(qsec) %>%
e_area(mpg) %>% # 添加阴影图
e_x_axis(min = 1) # 设定x轴的最小值

使用平滑线连接:

1
2
3
4
5
mtcars %>%
e_charts(wt) %>%
e_line(qsec, smooth = T) %>%
e_area(mpg, smooth = T) %>%
e_x_axis(min = 1)

修改图例:

1
2
3
4
5
6
7
mtcars %>%
e_charts(wt) %>%
e_line(qsec, smooth = T, name = "1/4 mile time") %>%
e_area(mpg, smooth = T, name = "Miles/gallon") %>%
e_axis(min = 1, name = "weight") %>%
e_title("Ol' mtcars example") %>%
e_theme("macarons")

图例和标题有点接近,让我们将图例移动到画布的另一部分:

1
2
3
4
5
6
7
8
mtcars %>%
e_charts(wt) %>%
e_line(qsec, smooth = T, name = "1/4 mile time") %>%
e_area(mpg, smooth = T, name = "Miles/gallon") %>%
e_axis(min = 1, name = "weight") %>%
e_title("Ol' mtcars example") %>%
e_theme("macarons") %>%
e_legend(bottom = 5)

触发

这里我们trigger = “axis”用来触发轴而不是单个数据点的提示。

更多选项可以参考这里:
https://ecomfe.github.io/echarts-doc/public/en/option.html#tooltip

1
2
3
4
5
6
7
8
9
mtcars %>%
e_charts(wt) %>%
e_line(qsec, smooth = T, name = "1/4 mile time") %>%
e_area(mpg, smooth = T, name = "Miles/gallon") %>%
e_axis(min = 1, name = "weight") %>%
e_title("Ol' mtcars example") %>%
e_theme("macarons") %>%
e_legend(bottom = 5) %>%
e_tooltip(trigger = "axis")

e_format_axis可以轻松地为轴标签添加后缀或前缀:

1
2
3
4
5
6
7
8
9
10
mtcars %>%
e_charts(wt) %>%
e_line(qsec, smooth = T, name = "1/4 mile time") %>%
e_area(mpg, smooth = T, name = "Miles/gallon") %>%
e_axis(min = 1, name = "weight") %>%
e_title("Ol' mtcars example") %>%
e_theme("macarons") %>%
e_legend(bottom = 5) %>%
e_tooltip(trigger = "axis") %>%
e_format_y_axis(suffix = "mpg")

动作和按钮

缩放

1
2
3
4
5
6
7
8
9
cars %>%
e_charts(dist) %>%
e_scatter(speed) %>%
e_datazoom() %>%
e_zoom(
dataZoomIndex = 0,
start = 20,
end = 40
)

按钮

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
cars %>%
e_charts(dist) %>%
e_scatter(speed) %>%
e_datazoom() %>%
e_zoom(
dataZoomIndex = 0,
start = 20,
end = 40,
btn = "BUTTON"
) %>%
e_button(
id = "BUTTON",
htmltools::tags$i(class = "fa fa-search"),
class = "btn btn-default",
"放大"
)


你也可以在文档的其他位置创建按钮,只要id匹配即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 首先构造数据
value <- rnorm(10, 10, 2)

nodes <- data.frame(
name = sample(LETTERS, 10),
value = value,
size = value,
grp = rep(c("grp1", "grp2"), 5),
stringsAsFactors = FALSE
)

edges <- data.frame(
source = sample(nodes$name, 20, replace = TRUE),
target = sample(nodes$name, 20, replace = TRUE),
stringsAsFactors = FALSE
)

e_charts() %>%
e_graph() %>%
e_graph_nodes(nodes, name, value, size, grp) %>%
e_graph_edges(edges, source, target) %>%
e_focus_adjacency(
seriesIndex = 0,
dataIndex = 4,
btn = "graphButton"
) %>%
e_button(
id = "graphButton",
class = "btn btn-danger",
htmltools::tags$a(
id = "graphButton",
class = "btn btn-primary",
"Highlight node #4"
)
)

也可以传递多个按钮并更改其位置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
iris %>%
group_by(Species) %>%
e_charts(Sepal.Length) %>%
e_line(Petal.Length) %>%
e_datazoom() %>%
e_highlight(series_name = "setosa", btn = "highlightButton") %>%
e_zoom(
dataZoomIndex = 0,
startValue = 4,
endValue = 6,
btn = "zoomBtn"
) %>%
e_button(
id = "zoomBtn",
position = "bottom",
class = "btn btn-primary",
"Zoom"
) %>%
e_button(
id = "highlightButton",
position = "bottom",
class = "btn btn-danger",
"highlight"
)

可以定义的动作

  • e_highlight并e_downplay突出或淡化一个系列。
  • e_showtip&e_hidetip显示或隐藏工具提示。
  • e_zoom 放大图形的一部分。
  • e_visual_map_range选择部分e_visual_map。
  • e_pie_select&e_pie_unselect选择或取消选择饼图切片。
  • e_focus_adjacency&e_unfocus_adjacency在图表上聚焦或取消聚焦节点。
  • e_legend_scroll,e_legend_toggle_select,e_legend_unselect和e_legend_select与图例交互。
  • e_restore 恢复图表。
  • e_map_select,e_map_unselect&e_map_toggle_select改变地图。
# R

评论

程振兴

程振兴 @czxa.top
截止今天,我已经在本博客上写了607.9k个字了!

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×