ECharts,是百度的一个开源的数据可视化工具,一个纯 Javascript 的图表库。echarts4r包是ECharts的R语言接口,目前可以从CRAN是直接安装。echarts4r语法结构简单,易用,可读性很好,是很好的交互式绘图包。
本文介绍echarts4r的常规交互式图形,交互式图形可以用在rmarkdown和shiny应用中。
安装包
install.packages("echarts4r")
散点图
echarts4r作图第一步用e_charts
函数创建一个echarts4r对象,函数第一个参数为数据,第二个参数为x轴数据,第二步以及后续都是用%>%
管道操作符来进一步作图。
绘制散点图,用iris
数据,x轴为Sepal.Length。y轴为Petal.Length,在e_scatter
中定义为serie
。通过group_by
根据Sepal.Length进行分组,在图中表现为不同颜色。散点大小通过size
参数设置。
library(echarts4r)
iris %>%
group_by(Species) %>%
e_charts(x = Sepal.Length) %>%
e_scatter(serie = Petal.Length, size = Sepal.Width)

scatter.png
对比一下ggplot2的散点图语法。
library(ggplot2)
iris %>%
ggplot(aes(x=Sepal.Length,y=Petal.Length,size=Sepal.Width,col=Species))+
geom_point()

ggplot2_scatter.png
柱状图
df %
e_charts(x) %>%
e_bar(y, name = "bar") %>%
e_title("Bar and step charts")

bar.png
极坐标图
df %>%
e_charts(x) %>%
e_polar() %>%
e_angle_axis(x) %>% # angle = x
e_radius_axis() %>%
e_bar(y, coord_system = "polar") %>%
e_scatter(z, coord_system = "polar")

radius.png
漏斗图
funnel %
e_charts() %>%
e_funnel(value, stage) %>%
e_title("Funnel")

funnel.png
热力图
v %
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("Heatmap")

heatMap.png
日历图
dates %
e_charts(date) %>%
e_calendar(range = "2018") %>%
e_heatmap(values, coord_system = "calendar") %>%
e_visual_map(max = 30) %>%
e_title("Calendar", "Heatmap")

calendar.png
仪表盘
e_charts() %>%
e_gauge(41, "PERCENT") %>%
e_title("Gauge")

gauge.png
雷达图
df %
e_charts(x) %>%
e_radar(y, max = 7, name = "radar") %>%
e_radar(z, max = 7, name = "chart") %>%
e_tooltip(trigger = "item")

radar.png
词云
words %
dplyr::arrange(-freq)
tf %>%
e_color_range(freq, color) %>%
e_charts() %>%
e_cloud(terms, freq, color, shape = "circle", sizeRange = c(3, 15)) %>%
e_title("Wordcloud", "Random strings")

cloud.png