R语言 交互式绘图echarts4r包初探
“ echarts4r 包是R 语言访问/调用百度ECharts的接口,语法结构简单,可读性强,是很好的交互式绘图包。”
01
—
打样
上图1
# install.packages("echarts4r")
library(echarts4r)
df <- data.frame(
x = seq(50),
y = rnorm(50, 10, 3),
z = rnorm(50, 11, 2),
w = rnorm(50, 9, 2)
)
# > head(df)
# x y z w
# 1 1 14.681910 8.655808 8.986928
# 2 2 8.738652 10.663773 13.174984
# 3 3 6.350329 12.576939 8.227128
# 4 4 10.699079 10.900659 10.503904
# 5 5 12.909827 10.253127 12.261691
# 6 6 7.376608 15.973790 8.305172
#图1
df %>%
e_charts(x) %>%
e_line(z) %>%
e_area(w) %>%
e_title("Line and area charts")
02
—
散点图/Scatter
上图2
上图3
# df <- data.frame(
# x = seq(50),
# y = rnorm(50, 10, 3),
# z = rnorm(50, 11, 2),
# w = rnorm(50, 9, 2)
# )
# 图2
df %>%
e_charts(x) %>% #初始化并设置x
e_scatter(y) #设置scatter类型和y
# 图3
df %>%
e_charts(x) %>% #初始化并设置x轴变量
e_scatter(y, z) ##设置scatter类型、y变量和点大小z
03
—
桑基图/Sankey
上图4
sankey <- data.frame(
source = c("a", "b", "c", "d", "c"),
target = c("b", "c", "d", "e", "e"),
value = ceiling(rnorm(5, 10, 1)),
stringsAsFactors = FALSE
)
sankey %>%
e_charts() %>%
e_sankey(source, target, value) %>%
e_title("Sankey chart")
04
—
三维/3D
上图5
#图5
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)), # scale size
dimension = 3 # third dimension 0 = x, y = 1, z = 2, size = 3
) %>%
e_visual_map(
color,
inRange = list(color = c('#bf444c', '#d88273', '#f6efa6')), # scale colors
dimension = 4, # third dimension 0 = x, y = 1, z = 2, size = 3, color = 4
bottom = 300 # padding to avoid visual maps overlap
)
参考:echarts4r说明书
https://echarts4r.john-coene.com/articles/chart_types.html