Chapter 3 常用图表类型
3.1 一个简单的例子
<- state.x77 %>%
df as.data.frame() %>%
::rownames_to_column("State")
tibble
%>%
df e_charts(State) %>% # 初始化图表,设置横轴
e_line(Population, smooth = T) %>% # 添加一条线图
e_area(Income ,smooth = TRUE, y_index = 1) %>% # 添加面积图,并设置双轴 y_index = 1添加y轴, 形成双轴
e_axis_labels(x = "State") %>% # 设置横轴名称
e_title("US States", "Population & Income", "biteam.dmall.com") %>% # 添加图表标题
e_legend(right = 0, show = T) %>% # 设置图例位置
e_tooltip(trigger = "axis") # 设置鼠标悬浮提示
3.3 stacked 堆叠
3.3.1 通过指定stack名称进行堆叠,相同名称的按顺序堆在一起
<- data.frame(
df x = LETTERS[1:10],
a = runif(10),
b = runif(10),
c = runif(10),
d = runif(10)
)
# stack名称相同的会按顺序堆叠起来, 不同的名称新增柱子不会堆叠
%>%
df e_charts(x) %>%
e_bar(a, stack = "grp") %>%
e_bar(b, stack = "grp") %>%
e_bar(c, stack = "grp2") %>%
e_bar(d, stack = "grp2") %>%
e_tooltip(trigger = 'axis')
3.4 散点图
我们使用鸢尾花数据集作示例,绘制花萼长度Sepal.Length和花瓣长度Petal.Length之间的散点关系 1. 将横坐标传Sepal.Length给e_charts(), 初始化图表 2. 绘制散点,将纵坐标Petal.Length传e_scatter() 因为是散点图,这里的横纵坐标调换一下也有意义
%>%
iris e_charts(Sepal.Length) %>%
e_scatter(Petal.Length)
默认的散点比较小,看起来不好看,通过symbol_size调整点的大小
%>%
iris e_charts(Sepal.Length) %>%
e_scatter(Petal.Length, symbol_size = 15)
希望按照鸢尾花的不同品种标上颜色,这里我们直接对数据按照Species进行分组,实现不同的品种不同的颜色
%>%
iris group_by(Species) %>%
e_charts(Sepal.Length) %>%
e_scatter(Petal.Length, Sepal.Width) %>% # 第一个参数是y轴, 第二个是点的大小
e_tooltip(trigger = 'axis')
使用e_effect_scatter()替换e_scatter()实现散点动效
%>%
iris group_by(Species) %>%
e_charts(Sepal.Length) %>%
e_effect_scatter(Petal.Length, Sepal.Width)
3.5 热力图
<- LETTERS[1:10]
v <- data.frame(
matrix x = sample(v, 300, replace = TRUE),
y = sample(v, 300, replace = TRUE),
z = rnorm(300, 10, 1),
stringsAsFactors = FALSE
%>%
) ::group_by(x, y) %>%
dplyr::summarise(z = sum(z)) %>%
dplyr::ungroup()
dplyr#> `summarise()` regrouping output by 'x' (override with `.groups` argument)
%>%
matrix e_charts(x) %>%
e_heatmap(y, z) %>%
e_visual_map(z)
3.5.1 日历
<- seq.Date(as.Date("2017-01-01"), as.Date("2018-12-31"), by = "day")
dates <- rnorm(length(dates), 20, 6)
values
<- data.frame(date = dates, values = values)
year
# year
%>%
year e_charts(date) %>%
e_calendar(range = "2018") %>%
e_heatmap(values, coord_system = "calendar") %>%
e_visual_map(max = 30) %>%
e_tooltip(trigger = 'axis')
3.5.2 多个日历
%>%
year ::mutate(year = format(date, "%Y")) %>% # get year from date
dplyrgroup_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("Calendar", "Heatmap")%>%
e_tooltip("item")
3.8 polar
<- data.frame(x = 1:10, y = seq(1, 20, by = 2))
df
%>%
df e_charts(x) %>%
e_line(y)
%>%
df e_charts(x) %>%
e_polar() %>%
e_angle_axis() %>%
e_radius_axis() %>%
e_line(y, coord_system = "polar", smooth = TRUE)
%>%
df e_charts(x) %>%
e_polar() %>%
e_angle_axis() %>%
e_radius_axis() %>%
e_bar(y, coord_system = "polar", smooth = TRUE)
3.9 flip_coords
data.frame(
x = LETTERS[1:5],
y = runif(5, 1, 15)
%>%
) e_charts(x) %>%
e_bar(y, name = "flipped") %>%
e_flip_coords() # flip axis
3.10 pie
%>%
mtcars head() %>%
::mutate(model = row.names(.)) %>%
dplyre_charts(model) %>%
e_pie(carb) %>%
e_title("Pie chart")
3.11 Rosetype
%>%
mtcars head() %>%
::mutate(model = row.names(.)) %>%
dplyre_charts(model) %>%
e_pie(carb, roseType = "radius")
3.12 sunburst
<- tibble(
df name = c("earth", "mars", "venus"),
value = c(30, 40, 30), # 1st level
itemStyle = tibble(color = c(NA, 'red', 'blue')),
children = list(
# earth的children
tibble(name = c("land", "ocean"),
value = c(10,20), # 2nd level
children = list(
tibble(name = c("forest", "river"),
value = c(3,7)), # 3rd level
tibble(name = c("fish", "kelp"),
value = c(10,5),
children = list(
tibble(name = c("shark", "tuna"),
value = c(2,6)), # 4th level
NULL # kelp
))
)),# mars的children
tibble(name = c("crater", "valley"), value = c(20,20)),
# venus的children
NULL # venus
)
)
df
## # A tibble: 3 × 4
## name value itemStyle$color children
## <chr> <dbl> <chr> <list>
## 1 earth 30 <NA> <tibble [2 × 3]>
## 2 mars 40 red <tibble [2 × 2]>
## 3 venus 30 blue <NULL>
%>%
df e_charts() %>%
e_sunburst() %>%
e_title("Sunburst")
3.13 tree
<- tibble(
df name = "earth", # 1st level
children = list(
tibble(name = c("land", "ocean"), # 2nd level
children = list(
tibble(name = c("forest", "river")), # 3rd level
tibble(name = c("fish", "kelp"),
children = list(
tibble(name = c("shark", "tuna"), # 4th level
NULL # kelp
))
)
))
)
)
df
## # A tibble: 1 × 2
## name children
## <chr> <list>
## 1 earth <tibble [2 × 2]>
%>%
df e_charts() %>%
e_tree()
%>%
df e_charts() %>%
e_tree(layout = "radial")
3.15 River
<- seq.Date(Sys.Date() - 30, Sys.Date(), by = "day")
dates
<- data.frame(
river dates = dates,
apples = runif(length(dates)),
bananas = runif(length(dates)),
pears = runif(length(dates))
)
%>%
river e_charts(dates) %>%
e_river(apples) %>%
e_river(bananas) %>%
e_river(pears) %>%
e_tooltip(trigger = "axis") %>%
e_title("River charts", "(Streamgraphs)") %>%
e_datazoom()
3.16 添加标志线、标志点;添加双x、双y坐标轴
%>%
USArrests ::mutate(
dplyrState = row.names(.),
Rape = -Rape
%>%
) e_charts(State) %>%
e_area(Murder) %>%
# e_bar(Rape, name = "Sick basterd") %>% # second x axis
e_bar(Rape, name = "Sick basterd", x_index = 1) %>% # second y axis
e_mark_line("Sick basterd", data = list(type = "average")) %>%
e_mark_point("Murder", data = list(type = "min"))
3.17 show labels
# 方式1
%>%
USArrests ::mutate(
dplyrState = row.names(.)
%>%
) ::slice(1:10) %>%
dplyre_charts(State) %>%
e_area(Murder, label = list(normal = list(show = TRUE)))
# 方式2
%>%
USArrests ::mutate(
dplyrState = row.names(.)
%>%
) ::slice(1:10) %>%
dplyre_charts(State) %>%
e_area(Murder) %>%
e_labels()
3.18 nested data
3.18.1 这里还通过e_add实现根据条件是否展示坐标
# add columns to iris
<- iris %>%
iris_dat ::mutate(
dplyrshow = TRUE, # to show the labels
fontSize = exp(Sepal.Length) / 10, # font size will correspond to Sepal.Length
color = sample(c("red", "black", "blue"), dplyr::n(), replace = TRUE) # assign a random color to the label
)
head(iris_dat)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species show fontSize color
## 1 5.1 3.5 1.4 0.2 setosa TRUE 16.402191 red
## 2 4.9 3.0 1.4 0.2 setosa TRUE 13.428978 black
## 3 4.7 3.2 1.3 0.2 setosa TRUE 10.994717 red
## 4 4.6 3.1 1.5 0.2 setosa TRUE 9.948432 red
## 5 5.0 3.6 1.4 0.2 setosa TRUE 14.841316 black
## 6 5.4 3.9 1.7 0.4 setosa TRUE 22.140642 black
%>%
iris_dat ::slice(1:10) %>% # simplify the graph.
dplyre_charts(Sepal.Width) %>%
e_line(Sepal.Length) %>%
e_add("label", show, fontSize, color) %>% # add our columns to "label"
e_x_axis(min = 2.5)
## Warning: 'e_add' is deprecated.
## Use 'e_add_nested' instead.
## See help("Deprecated") and help("echarts4r-deprecated").
3.19 漏斗
<- data.frame(
funnel stage = c("View", "Click", "Purchase"),
value = c(80, 30, 20),
color = c("blue", "red", "green")
)
%>%
funnel e_charts() %>%
e_funnel(value, stage) %>%
e_add("itemStyle", color)
## Warning: 'e_add' is deprecated.
## Use 'e_add_nested' instead.
## See help("Deprecated") and help("echarts4r-deprecated").
%>%
funnel e_charts() %>%
e_funnel(value, stage)
3.20 Rader
<- data.frame(
df 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, max = 7, name = "chart") %>%
e_tooltip(trigger = "item")
3.21 Wordcloud
<- function(n = 5000) {
words <- do.call(paste0, replicate(5, sample(LETTERS, n, TRUE), FALSE))
a paste0(a, sprintf("%04d", sample(9999, n, TRUE)), sample(LETTERS, n, TRUE))
}
<- data.frame(terms = words(100),
tf freq = rnorm(100, 55, 10)) %>%
::arrange(-freq)
dplyr
%>%
tf e_color_range(freq, color) %>%
e_charts() %>%
e_cloud(terms, freq, color, shape = "circle", sizeRange = c(3, 15)) %>%
e_title("Wordcloud", "Random strings")
3.22 Candlestick
library(quantmod)
# getSymbols("GS") #Goldman Sachs
# GS <- as.data.frame(GS)
# GS$date <- row.names(GS)
#
#
# GS %>%
# e_charts(date) %>%
# e_candle(GS.Open, GS.Close, GS.Low, GS.High, name = "Goldman Sachs") %>%
# e_datazoom(type = "slider") %>%
# e_title("Candlestick chart", "Quantmod data")
3.23 sankey 桑基
<- data.frame(
sankey source = c("a", "b", "c", "d", "c"),
target = c("b", "c", "d", "e", "e"),
value = ceiling(rnorm(5, 10, 1)),
stringsAsFactors = FALSE
)
sankey
## source target value
## 1 a b 9
## 2 b c 10
## 3 c d 10
## 4 d e 12
## 5 c e 10
%>%
sankey e_charts() %>%
e_sankey(source, target, value) %>%
e_title("Sankey chart")
3.24 parallel
<- data.frame(
df price = rnorm(5, 10),
amount = rnorm(5, 15),
letter = LETTERS[1:5]
)
df
## price amount letter
## 1 8.440435 15.38694 A
## 2 11.042150 15.24600 B
## 3 11.009427 13.39779 C
## 4 12.122676 15.83889 D
## 5 10.493865 16.11394 E
%>%
df e_charts() %>%
e_parallel(price, amount, letter) %>%
e_title("Parallel chart")