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

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

本文学习了echarts4r的高级用法,不知何故,教程上的有些图表无法正常渲染,已抛弃。

分组

1
2
3
4
5
6
library(echarts4r)
iris %>%
group_by(Species) %>%
e_charts(Sepal.Length) %>%
e_line(Sepal.Width) %>%
e_title("Grouped data")

散点图

1
2
3
4
iris %>%
group_by(Species) %>%
e_charts(Sepal.Length) %>%
e_scatter(Petal.Length, Sepal.Width)

还可以自己定义缩放:

1
2
3
4
5
6
7
my_scale <- function(x){
scales::rescale(x, to = c(5,30))
}
iris %>%
group_by(Species) %>%
e_charts(Sepal.Length) %>%
e_scatter(Petal.Length, size = Sepal.Width, scale = my_scale)

如果没有传递size参数,可以使用symbol_size定义散点大小:

1
2
3
4
5
iris %>%
group_by(Species) %>%
e_charts(Sepal.Length) %>%
e_scatter(Petal.Length, symbol_size = 15) %>%
e_theme("macarons")

当size参数和symbol_size参数同时被传递的时候,symbol_size会是两者的乘数:

1
2
3
4
iris %>%
group_by(Species) %>%
e_charts(Sepal.Length) %>%
e_scatter(Petal.Length, Sepal.Width, scale = my_scale, symbol_size = 1.2)

需要注意的是,重新缩放意味着你还要重新缩放visual_map:

1
2
3
4
5
mtcars %>%
e_charts(mpg) %>%
e_scatter(qsec, wt, scale = e_scale) %>%
e_legend(show = F) %>%
e_visual_map(wt, scale = e_scale) %>% e_theme("macarons")

或者这样:

1
2
3
4
5
mtcars %>%
e_charts(mpg) %>%
e_scatter(qsec, wt, scale = e_scale) %>%
e_legend(show = F) %>%
e_visual_map(min = 1, max = 20) %>% e_theme("macarons")

坐标系

热力图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 首先是常规热力图
v <- LETTERS[1:10]
matrix <- data.frame(
x = sample(v, 300, replace = T),
y = sample(v, 300, replace = T),
z = rnorm(300, 10, 1),
stringsAsFactors = F
) %>%
dplyr::group_by(x, y) %>%
dplyr::summarise(z = sum(z)) %>%
dplyr::ungroup()

matrix %>%
e_charts(x) %>%
e_heatmap(y, z) %>%
e_visual_map(z)

日历热力图

1
2
3
4
5
6
7
8
9
dates <- seq.Date(as.Date("2018-01-01"), as.Date("2018-12-31"), by = "day")
values = rnorm(length(dates), 20, 6)
year <- data.frame(date = dates, value = values)

year %>%
e_charts(date) %>%
e_calendar(range = "2018") %>%
e_heatmap(value, coord_system = "calendar") %>%
e_visual_map(max = 30)

另一个例子是将2D笛卡尔坐标系上的一条直线映射到极坐标系中:

1
2
3
4
5
df <- data.frame(x = 1:10, y = seq(1, 20, by = 2))
# 首先是2D笛卡尔坐标系
df %>%
e_charts(x) %>%
e_line(y)

然后是极坐标系:

1
2
3
4
5
6
df %>%
e_charts(x) %>%
e_polar() %>%
e_angle_axis() %>%
e_radius_axis() %>%
e_line(y, coord_system = "polar", smooth = T)

自定义坐标轴

通过指定坐标轴的索引可以使用多个坐标轴:

1
2
3
4
5
USArrests %>%
e_charts(Assault) %>%
e_line(Murder, smooth = T) %>%
e_line(Rape, y_index = 1) %>%
e_y_axis(splitLine = list(show = F)) # 隐藏第一个y轴的分割线

翻转坐标轴

1
2
3
4
5
6
7
8
data.frame(
x = LETTERS[1:5],
y = runif(5, 1, 15)
) %>%
e_charts(x) %>%
e_bar(y, name = "flipped") %>%
e_theme("macarons") %>%
e_flip_coords()

添加点标签和辅助线

1
2
3
4
5
6
7
8
9
10
11
USArrests %>%
dplyr::mutate(
State = row.names(.),
Rape = - Rape
) %>%
e_charts(State) %>%
e_area(Murder) %>%
e_bar(Rape, name = "Sick basterd", x_index = 1) %>%
e_mark_line("Sick basterd", data = list(type = "average")) %>%
e_mark_point("Murder", data = list(type = "min")) %>%
e_theme("macarons")

树图

常规树图

1
2
3
4
5
6
7
8
9
10
11
df <- data.frame(
parent = c("earth","earth","forest","forest","ocean","ocean","ocean","ocean"),
child = c("ocean","forest","tree","sasquatch","fish","seaweed","mantis shrimp","sea monster")
)

df %>%
e_charts() %>%
e_tree(
parent = parent,
child = child
)

径向树图

1
2
3
4
5
6
7
df %>%
e_charts() %>%
e_tree(
parent = parent,
child = child,
layout = "radial"
)

显示标签

1
2
3
4
5
6
7
8
9
USArrests %>%
dplyr::mutate(
State = row.names(.)
) %>%
dplyr::slice(1:10) %>%
e_charts(State) %>%
e_area(Murder, smooth = T) %>%
e_labels() %>%
e_theme("macarons")

嵌套数据

下面的代码中在iris数据集里添加一些列,然后用e_add来定义数据集的label:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
iris_dat <- iris %>%
dplyr::mutate(
show = T,
fontSize = exp(Sepal.Length)/10,
color = sample(c("red", "black", "blue"), n(), replace = T)
)

iris_dat %>%
dplyr::slice(1:10) %>%
e_charts(Sepal.Width) %>%
e_line(Sepal.Length) %>%
e_add("label", show, fontSize, color) %>%
e_x_axis(min = 2.8) %>%
e_y_axis(min = 4)

另外一个例子是漏斗图:

1
2
3
4
5
6
7
8
9
10
funnel <- data.frame(
stage = c("View", "Click", "Purchase"),
value = c(80, 30, 20),
color = c("blue", "red", "green")
)

funnel %>%
e_charts() %>%
e_funnel(value, stage) %>%
e_add("itemStyle", color)

# R

评论

程振兴

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

Your browser is out-of-date!

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

×