First, you should load recharts
:
library(recharts)
Bar plot includes 3 basic types:
The keys are:
x
and numeric y
x
(and series
) can only have one y
data pointechartr(data, x, <y>, <series>, <t>, <type>, <subtype>)
Arg | Requirement |
---|---|
data |
source data in the form of data.frame |
x |
|
y |
|
series |
data series variable which will be coerced to factors. Only the first one is accepted if multiple variables are provided. |
t |
timeline variable which will be coerced to factors. Only the first one is accepted if multiple variables are provided. |
type |
|
subtype |
|
Let’s look into Titanic
dataset embeded in the package datasets
. The survival count by cabin class is shown as below:
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
could be ‘hbar’, ‘bar’ or ‘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')
Compared to hbar, you just set subtype
‘stack’.
Grammar for singular and multiple series are similar to legacy hbar chart.
echartr(titanic, Class, Count, Survived, type='hbar', subtype='stack') %>%
setTitle('Titanic: Survival Outcome by Cabin Class')
Tornado chart is a special case. The keys are:
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
Of course we need to adjust the axis a little bit as well. Y-axis should intersect x-axis at zero and the label of x-axis should be absolute values (a little bit complex, you’d better know a little bit JaveScript).
g %>% setYAxis(axisLine=list(onZero=TRUE)) %>%
setXAxis(axisLabel=list(
formatter=JS('function (value) {return Math.abs(value);}')
))
So when you apply ‘hbar’ to type
and ‘stack’ to subtype
, you yield a ‘population pyramid’ as usually used in sociology.
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);}')
))
We need another variable as timeline. Let’s say, ‘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")
Compared to hbar, you just set type
‘vbar’, or ‘column’.
Grammar for singular and multiple series are similar to legacy hbar chart.
echartr(titanic, Class, Count, Survived, type='column') %>%
setTitle('Titanic: Survival Outcome by Cabin Class')
Compared to vbar, you just set subtype
‘stack’.
Grammar for singular and multiple series are similar to legacy hbar chart.
echartr(titanic, Class, Count, Survived, type='column', subtype='stack') %>%
setTitle('Titanic: Survival Outcome by Cabin Class')
Histogram is a special form of bar chart, in which you only provide numeric x
variable.
type
could be ‘histogram’, ‘hist’. Run setTooltip(formatter='none')
to apply the default tooltip template in Echarts.
The barWidth can not be autosized in Echarts2. Please assign a suitable value yourself.
echartr(iris, Sepal.Width, width=600) %>%
setTitle('Iris: Histogram of Sepal.Width') %>%
setTooltip(formatter='none') %>% setSeries(1, barWidth=500/13)
Sometimes you want a density historam, then set 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)
Then you can configure the widgets, add markLines and/or markPoints, fortify the chart.
You can refer to related functions to play around on your own.