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

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

本文是:

的学习笔记。介绍了echarts4r支持的 其它图表类型。

web GL

WebGL(全写Web Graphics Library)是一种3D绘图协议,这种绘图技术标准允许把JavaScript和OpenGL ES 2.0结合在一起,通过增加OpenGL ES 2.0的一个JavaScript绑定,WebGL可以为HTML5 Canvas提供硬件3D加速渲染,这样Web开发人员就可以借助系统显卡来在浏览器里更流畅地展示3D场景和模型了,还能创建复杂的导航和数据视觉化。显然,WebGL技术标准免去了开发网页专用渲染插件的麻烦,可被用于创建具有复杂3D结构的网站页面,甚至可以用来设计3D网页游戏等等。
当您有大型数据集进行绘图时,webGL是理想的选择。

散点

R
1
2
3
4
5
6
7
8
9
10
11
12
13
library(echarts4r)
head(quakes)
quakes %>%
e_charts(long) %>%
e_geo(
boundingCoords = list(
c(185, -10),
c(165, -40)
)
) %>%
e_scatter_gl(lat, depth) %>%
e_visual_map() %>%
e_theme("macarons")

Graph GL

R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
nodes <- data.frame(
name = paste0(LETTERS, 1:300),
value = rnorm(300, 10, 2),
size = rnorm(300, 10, 2),
grp = rep(c("grp1", "grp2", "grp3"), 100),
stringsAsFactors = F
)
edges <- data.frame(
source = sample(nodes$name, 400, replace = T),
target = sample(nodes$name, 400, replace = T),
stringsAsFactors = F
)

e_charts() %>%
e_graph_gl() %>%
e_graph_nodes(nodes, name, value, size, grp) %>%
e_graph_edges(edges, source, target) %>%
e_theme("macarons")

Flow GL

R
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
vectors <- expand.grid(x = -3:3, y = -3:3)
mu <- 1
vectors$sx <- vectors$y
vectors$sy <- mu * (1 - vectors$x^2) * vectors$y - vectors$x
vectors$color <- log10(runif(nrow(vectors), 1, 10))

vectors %>%
e_charts(x) %>%
e_flow_gl(y, sx, sy, color) %>%
e_visual_map(
min = 0, max = 1,
dimension = 4,
show = F,
inRange = list(
color = c(
'#313695', '#4575b4',
'#74add1', '#abd9e9',
'#e0f3f8', '#ffffbf',
'#fee090', '#fdae61',
'#f46d43', '#d73027',
'#a50026')
)
) %>%
e_x_axis(
splitLine = list(show = F)
) %>%
e_y_axis(
splitLine = list(show = F)
) %>%
e_theme("macarons")

也可以基于不同的坐标系绘制:

R
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
latlong <- seq(-180, 180, by = 5)
wind = expand.grid(lng = latlong, lat = latlong)
wind$slng <- rnorm(nrow(wind), 0, 200)
wind$slat <- rnorm(nrow(wind), 0, 200)
wind$color <- abs(wind$slat) - abs(wind$slng)
trans <- list(opacity = 0.5)

wind %>%
e_charts(lng, backgroundColor = '#333') %>%
e_geo(
itemStyle = list(
normal = list(
areaColor = "#323c48",
borderColor = "#111"
)
)
) %>%
e_flow_gl(lat, slng, slat, color,
coord_system = 'geo',
itemStyle = trans,
particleSize = 2) %>%
e_visual_map(
color,
dimension = 4,
show = F,
inRange = list(
color = c(
'#313695', '#4575b4',
'#74add1', '#abd9e9',
'#e0f3f8', '#ffffbf',
'#fee090', '#fdae61',
'#f46d43', '#d73027',
'#a50026')
)
)

统计图表

置信区间

R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
library(echarts4r.suite)
# 置信区间
data <- jsonlite::fromJSON(
paste0(
"https://ecomfe.github.io/echarts-examples/public/",
"data/asset/data/confidence-band.json"
)
)
data %>%
dplyr::mutate(
date = as.Date(date, "%Y-%m-%d"),
l = l + value,
u = u - value
) %>%
e_charts(date) %>%
e_line(value, symbol = "none") %>%
e_area(l, stack = "stack", symbol = "none") %>%
e_area(u, stack = "stack", symbol = "none") %>%
e_theme("macarons")

箱线图

R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
df <- data.frame(
x = c(
rnorm(100),
runif(100, -5, 10),
rnorm(100, 10, 3)
),
grp = c(
rep(LETTERS[1], 100),
rep(LETTERS[2], 100),
rep(LETTERS[3], 100)
)
)

df %>%
group_by(grp) %>%
e_charts() %>%
e_boxplot(x) %>%
e_theme("macarons")

直方图

R
1
2
3
4
5
6
7
8
9
10
df <- data.frame(
x = 1:100,
y = rnorm(100, 20, 12)
)

df %>%
e_charts() %>%
e_histogram(y) %>%
e_tooltip() %>%
e_theme("macarons")

密度图

R
1
2
3
4
5
6
7
8
df %>%
e_charts() %>%
e_histogram(y) %>%
e_density(y, name = "密度曲线",
areaStyle = list(opacity = 0.4),
smooth = T, y_index = 1) %>%
e_tooltip() %>%
e_theme("macarons")

线性拟合

R
1
2
3
4
5
6
7
iris %>%
group_by(Species) %>%
e_charts(Sepal.Length) %>%
e_line(Sepal.Width) %>%
e_lm(Sepal.Width ~ Sepal.Length) %>%
e_x_axis(min = 4) %>%
e_theme("macarons")

高次拟合

R
1
2
3
4
5
mtcars %>%
e_charts(disp) %>%
e_scatter(mpg, qsec) %>%
e_loess(mpg ~ disp) %>%
e_theme("macarons")

画报

内置图标

R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
y <- rnorm(10, 10, 2)
df <- data.frame(
x = 1:10,
y = y,
z = y - rnorm(10, 5, 1)
)

df %>%
e_charts(x) %>%
e_bar(z, barWidth = 10) %>%
e_pictorial(y, symbol = "rect",
symbolRepeat = T,
z = -1, symbolSize = c(10, 4)) %>%
e_theme("westeros") %>%
e_title("内置图标")

SVG path

R
1
2
3
4
5
6
7
8
9
10
11
12
path = "path://M0,10 L10,10 C5.5,10 5.5,5 5,0 C4.5,5 4.5,10 0,10 z"
style = list(
normal = list(opacity = 0.5),
emphasis = list(opacity = 1)
)
df %>%
e_charts(x) %>%
e_pictorial(y, symbol = path,
barCategoryGap = "-130%",
itemStyle = style) %>%
e_title("SVG path") %>%
e_theme("macarons")

使用图片

R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
qomo <- paste0(
"https://ecomfe.github.io/echarts-examples/public/",
"data/asset/img/hill-Qomolangma.png"
)

kili <- paste0(
"https://ecomfe.github.io/echarts-examples/public/",
"data/asset/img/hill-Kilimanjaro.png"
)

data <- data.frame(
x = c("珠穆朗玛峰", "乞力马扎罗山"),
value = c(8844, 5895),
symbol = c(paste0("image://", qomo),
paste0("image://", kili))
)
data %>%
e_charts(x) %>%
e_pictorial(value, symbol) %>%
e_legend(F) %>%
e_title("图片", "山高")

3D

3D柱状图

R
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
v <- LETTERS[1:10]
matrix <- data.frame(
x = sample(v, 300, replace = TRUE),
y = sample(v, 300, replace = TRUE),
z1 = rnorm(300, 10, 1),
z2 = rnorm(300, 10, 1),
stringsAsFactors = FALSE
) %>%
dplyr::group_by(x, y) %>%
dplyr::summarise(
z1 = sum(z1),
z2 = sum(z2)
) %>%
dplyr::ungroup()
trans <- list(opacity = 0.4)
emphasis <- list(itemStyle = list(
color = "#313695"
))

matrix %>%
e_charts(x) %>%
e_bar_3d(y, z1, stack = "stack",
name = "序列1",
itemStyle = trans,
emphasis = emphasis) %>%
e_bar_3d(y, z2, stack = "stack",
name = "序列2",
itemStyle = trans,
emphasis = emphasis) %>%
e_legend()

3D散点图

R
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
v <- LETTERS[1:10]
matrix <- data.frame(
x = sample(v, 300, replace = TRUE),
y = sample(v, 300, replace = TRUE),
z = rnorm(300, 10, 1),
color = rnorm(300, 10, 1),
size = rnorm(300, 10, 1),
stringsAsFactors = FALSE
) %>%
dplyr::group_by(x, y) %>%
dplyr::summarise(
z = sum(z),
color = sum(color),
size = sum(size)
) %>%
dplyr::ungroup()

matrix %>%
e_charts(x) %>%
e_scatter_3d(y, z, size, color) %>%
e_visual_map(
size,
inRange = list(symbolSize = c(1, 30)),
dimension = 3
) %>%
e_visual_map(
color,
inRange = list(
color = c('#bf444c', '#d88273', '#f6efa6')
),
dimension = 4,
bottom = 300
)

3D线

R
1
2
3
4
5
6
7
8
9
10
11
df <- data.frame(
x = 1:100,
y = runif(100, 10, 25),
z = rnorm(100, 100, 50)
)

df %>%
e_charts(x) %>%
e_line_3d(y, z, smooth = TRUE) %>%
e_visual_map() %>%
e_theme("macarons")

# R

评论

程振兴

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

Your browser is out-of-date!

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

×