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

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

本文是学习Chart Types • echarts4r的笔记,介绍了echarts4r包支持的几种图表类型。

线图和区域图

1
2
3
4
5
6
7
8
9
10
11
12
library(echarts4r)
df <- data.frame(
x = seq(50),
y = rnorm(50, 10, 3),
z = rnorm(50, 11, 2),
w = rnorm(50, 9, 2)
)
df %>%
e_charts(x) %>%
e_line(z) %>%
e_area(w) %>%
e_title("线图和区域图")

条形图和阶梯图

1
2
3
4
5
df %>%
e_charts(x) %>%
e_bar(y, name = "序列1") %>%
e_step(z, name = "序列2") %>%
e_title("条形图和阶梯图")

散点图

1
2
3
4
5
df %>%
e_charts(x) %>%
e_scatter(y, z) %>%
e_visual_map(z, scale = e_scale) %>%
e_legend(F)

效果散点图

1
2
3
4
5
6
df %>%
head(10) %>%
e_charts(x) %>%
e_effect_scatter(y, z) %>%
e_visual_map(z) %>%
e_legend(F)

极坐标图

1
2
3
4
5
6
7
df %>%
e_charts(x) %>%
e_polar() %>%
e_angle_axis(x) %>%
e_radius_axis() %>%
e_bar(y, coord_system = 'polar') %>%
e_scatter(z, coord_system = 'polar')

径向图

1
2
3
4
5
6
7
8
df %>%
head(10) %>%
e_charts(x) %>%
e_polar() %>%
e_angle_axis() %>%
e_radius_axis(x) %>%
e_bar(y, coord_system = 'polar') %>%
e_scatter(z, coord_system = 'polar')

蜡烛图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
library(quantmod)
getSymbols("000001.SS")
SZ <- as.data.frame(`000001.SS`)
SZ$date <- row.names(SZ)
names(SZ) <- c("open", "high",
"low", "close",
"volume", "adjusted",
"date")
SZ %>%
e_charts(date) %>%
e_candle(open, close, low, high,
name = "上证综指") %>%
e_datazoom(type = "slider") %>%
e_title("上证综指蜡烛图", "数据来源:Quantmod")

漏斗图

1
2
3
4
5
6
7
8
9
funnel <- data.frame(
stage = c("浏览", "点击", "购买"),
value = c(80, 30, 20)
)
funnel %>%
e_charts() %>%
e_funnel(values = value,
labels = stage) %>%
e_title("漏斗图")

桑基图

1
2
3
4
5
6
7
8
9
10
sankey <- data.frame(
source = c("a", "b", "c", "d", "c"),
target = c("b", "c", "d", "e", "e"),
value = ceiling(rnorm(5, 10, 1)),
stringsAsFactors = F
)
sankey %>%
e_charts() %>%
e_sankey(source, target, value) %>%
e_title("桑基图")

热力图

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) %>%
e_title("热力图")

平行坐标轴图

1
2
3
4
5
6
7
8
9
df <- data.frame(
price = rnorm(5, 10),
amount = rnorm(5, 15),
letter = LETTERS[1:5]
)
df %>%
e_charts() %>%
e_parallel(price, amount, letter) %>%
e_title("平行坐标轴图")

饼图

1
2
3
4
5
6
mtcars %>%
head() %>%
dplyr::mutate(model = row.names(.)) %>%
e_charts(model) %>%
e_pie(carb) %>%
e_title("饼图")

甜甜圈图

1
2
3
4
5
6
mtcars %>%
head() %>%
dplyr::mutate(model = row.names(.)) %>%
e_charts(model) %>%
e_pie(carb, radius = c("50%", "70%")) %>%
e_title("甜甜圈图")

玫瑰图

1
2
3
4
5
mtcars %>%
head() %>%
dplyr::mutate(model = row.names(.)) %>%
e_charts(model) %>%
e_pie(hp, roseType = 'radius')

旭日图

1
2
3
4
5
6
7
8
9
10
df <- data.frame(
parent = c("地球", "地球", "地球",
"火星", "火星"),
child = c("森林", "海洋", "冰川", "elon", "curiosity"),
value = ceiling(rnorm(5, 10, 2))
)
df %>%
e_charts() %>%
e_sunburst(parent, child, value) %>%
e_title("旭日图")

树状图

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

tree %>%
e_charts() %>%
e_tree(parent, child) %>%
e_title("树状图")

树图

1
2
3
4
5
6
7
8
9
10
tm <- data.frame(
parent = c("earth", "earth", "earth", "mars", "mars"),
child = c("forest", "ocean", "iceberg", "elon", "curiosity"),
value = ceiling(rnorm(5, 10, 2))
)

tm %>%
e_charts() %>%
e_treemap(parent, child, value) %>%
e_title("树图")

河流图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
dates <- seq.Date(Sys.Date() - 30, Sys.Date(), by = "day")
rivers <- data.frame(
dates = dates,
apples = runif(length(dates)),
bananas = runif(length(dates)),
pears = runif(length(dates))
)

rivers %>%
e_charts(dates) %>%
e_river(apples) %>%
e_river(bananas) %>%
e_river(pears) %>%
e_tooltip(trigger = "axis") %>%
e_title("河流图", "(溪流图)") %>%
e_theme("macarons")

日历图

1
2
3
4
5
6
7
8
9
10
11
12
dates <- seq.Date(as.Date("2017-01-01"), as.Date("2018-12-31"), by = "day")
values <- rnorm(length(dates), 20, 6)
year <- data.frame(
date = dates,
values = values
)
year %>%
e_charts(date) %>%
e_calendar(range = "2018") %>%
e_heatmap(values, coord_system = "calendar") %>%
e_visual_map(max = 30) %>%
e_title("日历图", "热力图")

多年的日历图:

1
2
3
4
5
6
7
8
9
10
11
year %>%
dplyr::mutate(year = format(date, "%Y")) %>%
group_by(year) %>%
e_charts(date) %>%
e_calendar(range = "2017", top = 40) %>%
e_calendar(range = "2018", top = 260) %>%
e_heatmap(values, coord_system = "calendar") %>%
e_visual_map(max = 30) %>%
e_title("日历图", "热力图") %>%
e_tooltip("item") %>%
e_theme("macarons")

仪表盘

1
2
3
e_charts() %>%
e_gauge(41, "百分比") %>%
e_title("仪表盘")

雷达图

1
2
3
4
5
6
7
8
9
10
11
df <- data.frame(
x = LETTERS[1:5],
y = runif(5, 1, 5),
z = runif(5, 3, 7)
)

df %>%
e_charts(x) %>%
e_radar(y, max = 7, name = "radar") %>%
e_radar(z, nax = 7, name = "chart") %>%
e_tooltip(trigger = "item")

词云图

1
2
3
4
5
6
7
8
9
library(wordcloud2)
head(demoFreqC)
demoFreqC %>%
e_color_range(V1, color) %>%
e_charts() %>%
e_cloud(V2, V1, shape = "circle",
sizeRange = c(6, 15)) %>%
e_title("词云图") %>%
e_theme("macarons")

水球图

1
2
3
4
5
6
liquid <- data.frame(val = c(
0.6, 0.5, 0.4
))
liquid %>%
e_charts() %>%
e_liquid(val)

# R

评论

程振兴

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

Your browser is out-of-date!

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

×