首先,加载recharts
:
library(recharts)
条图Bar plot包含3种基本类型:
关键是:
x
和数值型y
x
各水平(和series
各水平)的每一个组合只能对应于一个y
数据点echartr(data, x, <y>, <series>, <t>, <type>, <subtype>)
参数 | 要求 |
---|---|
data |
数据框格式的源数据 |
x |
|
y |
|
series |
数据系列,转为因子处理。如提供多个变量,只传入第一个。 |
t |
时间轴变量,转为因子处理。如提供多个变量,只传入第一个。 |
type |
|
subtype |
|
让我们看一下datasets
包自带的Titanic
数据集。不同舱位的生存人数如下:
titanic <- data.table::melt(apply(Titanic, c(1,4), sum))
names(titanic) <- c('Class', 'Survived', 'Count')
knitr::kable(titanic)
Class | Survived | Count |
---|---|---|
1st | No | 122 |
2nd | No | 167 |
3rd | No | 528 |
Crew | No | 673 |
1st | Yes | 203 |
2nd | Yes | 118 |
3rd | Yes | 178 |
Crew | Yes | 212 |
type
可以是’hbar’、‘bar’或’auto’。
echartr(titanic[titanic$Survived=='Yes',], Class, Count) %>%
setTitle('Titanic: N Survival by Cabin Class')
echartr(titanic, Class, Count, Survived) %>%
setTitle('Titanic: Survival Outcome by Cabin Class')
相比于hbar,你需要设置subtype
为’stack’。
单系列和多系列堆积条图的实现语法和普通条图类似。
echartr(titanic, Class, Count, Survived, type='hbar', subtype='stack') %>%
setTitle('Titanic: Survival Outcome by Cabin Class')
龙卷风图是条图的特例。关键是:
titanic_tc <- titanic
titanic_tc$Count[titanic_tc$Survived=='No'] <-
- titanic_tc$Count[titanic_tc$Survived=='No']
g <- echartr(titanic_tc, Class, Count, Survived, type='hbar') %>%
setTitle("Titanic: Survival Outcome by Cabin Class")
g
当然,我们还得微调一下坐标轴。Y轴应该和x轴交会于零点,且x轴标签都要取绝对值 (略有点复杂,需要懂一点JaveScript)。
g %>% setYAxis(axisLine=list(onZero=TRUE)) %>%
setXAxis(axisLabel=list(
formatter=JS('function (value) {return Math.abs(value);}')
))
如果设type
为’hbar’,subtype
为’stack’,就得到了社会学中常用的人口学金字塔。
echartr(titanic_tc, Class, Count, Survived, type='hbar', subtype='stack') %>%
setTitle("Titanic: Survival Outcome by Cabin Class") %>%
setYAxis(axisLine=list(onZero=TRUE)) %>%
setXAxis(axisLabel=list(
formatter=JS('function (value) {return Math.abs(value);}')
))
需要一个时间轴变量。不妨用’sex’变量。
titanic_sex <- data.table::melt(apply(Titanic, c(1,2,4), sum))
names(titanic_sex)[4] <- "Count"
knitr::kable(titanic_sex)
Class | Sex | Survived | Count |
---|---|---|---|
1st | Male | No | 118 |
2nd | Male | No | 154 |
3rd | Male | No | 422 |
Crew | Male | No | 670 |
1st | Female | No | 4 |
2nd | Female | No | 13 |
3rd | Female | No | 106 |
Crew | Female | No | 3 |
1st | Male | Yes | 62 |
2nd | Male | Yes | 25 |
3rd | Male | Yes | 88 |
Crew | Male | Yes | 192 |
1st | Female | Yes | 141 |
2nd | Female | Yes | 93 |
3rd | Female | Yes | 90 |
Crew | Female | Yes | 20 |
echartr(titanic_sex, Class, Count, Survived, t=Sex, type='bar') %>%
setTitle("Titanic: Survival Outcome by Cabin Class Across Sex")
相比于hbar,需要设type
为’vbar’或’column’。
单系列或多系列柱图的实现语法和普通条图类似。
echartr(titanic, Class, Count, Survived, type='column') %>%
setTitle('Titanic: Survival Outcome by Cabin Class')
相比于vbar,需要设subtype
为’stack’。
单系列或多系列柱图的实现语法和普通条图类似。
echartr(titanic, Class, Count, Survived, type='column', subtype='stack') %>%
setTitle('Titanic: Survival Outcome by Cabin Class')
直方图是柱图的特例,只需要提供一个数值型x
变量。
type
可以是’histogram’、‘hist’。setTooltip(formatter='none'
调用默认的tooltip模板。
Echarts2无法自适应设定barWidth,所以你需要自己设定一个合理的数值。
echartr(iris, Sepal.Width, width=600) %>%
setTitle('Iris: Histogram of Sepal.Width') %>%
setTooltip(formatter='none') %>% setSeries(1, barWidth=500/13)
有时需要一幅密度直方图,那么设subtype
为’density’。
echartr(iris, Sepal.Width, type='hist', subtype='density', width=600) %>%
setTitle('Iris: Histogram of Sepal.Width') %>% setYAxis(name="Density") %>%
setTooltip(formatter='none') %>% setSeries(1, barWidth=500/13)
接下来可以配置控件、添加标注点/标注线,以及美化成图。
参考相关函数,尽情探索吧。