ECharts,一個使用 JavaScript 實現的開源可視化庫,可以流暢的運行在 PC 和移動設備上,兼容當前絕大部分瀏覽器(IE8/9/10/11,Chrome,Firefox,Safari等),底層依賴矢量圖形庫 ZRender,提供直觀,交互豐富,可高度個性化定制的數據可視化圖表。
echarts4r 包是 ECharts 的 R 語言接口,目前可以從 CRAN 是直接安裝。echarts4r 語法結構簡單,易用,可讀性很好,是很好的交互式繪圖包。

安裝echarts4r包,具體操作見上篇 R包 安裝教程
教程下載:https://cran.r-project.org/web/packages/echarts4r/index.html
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)

對比一下 ggplot2 的散點圖語法及效果
library(ggplot2) iris %>% ggplot(aes(x=Sepal.Length,y=Petal.Length,size=Sepal.Width,col=Species))+ geom_point()

柱狀圖
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_bar(y, name = "bar") %>%
e_title("Bar and step charts")

熱力圖
library(echarts4r)
v <- LETTERS[1:10]
matrix <- data.frame(
x = sample(v, 300, replace = TRUE),
y = sample(v, 300, replace = TRUE),
z = rnorm(300, 10, 1),
stringsAsFactors = FALSE
) %>%
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")

儀表盤
library(echarts4r)
e_charts() %>%
e_gauge(41, "PERCENT") %>%
e_title("Gauge")

詞雲
library(echarts4r)
words <- function(n = 5000) {
a <- do.call(paste0, replicate(5, sample(LETTERS, n, TRUE), FALSE))
paste0(a, sprintf("%04d", sample(9999, n, TRUE)), sample(LETTERS, n, TRUE))
}
tf <- data.frame(terms = words(100),
freq = rnorm(100, 55, 10)) %>%
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")

echarts4r 教程下載:https://cran.r-project.org/web/packages/echarts4r/index.html
