First, you should load recharts:

library(recharts)

1 Introduction

Bar plot includes 3 basic types:

  • horizontal bar: bar|hbar
  • vertical bar: column|vbar
  • histogram: histogram|hist

The keys are:

  • character x and numeric y
  • each combination level of x (and series) can only have one y data point
  • Tiled and stacked bar/column charts can shift from one to another by clicking type shift buttons in the toolbox widget.

2 Function Call

echartr(data, x, <y>, <series>, <t>, <type>, <subtype>)
Arg Requirement

data

source data in the form of data.frame

x

  • character independent variable. Other type will be coerced to factors. Only the first one is accepted if multiple variables are provided.
  • numeric variable and y is omitted, in which case comes up with a histogram.

y

  • numeric dependent variable. Only the first one is accepted if multiple variables are provided.
  • If y is omitted, and x is numeric, a histogram will be displayed.

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

  • x is character and y is numeric, or ‘x’ is numeric and ‘y’ is omitted, you can omit ‘type’ or assign it ‘auto’.
  • You can also assign it ‘bar’, ‘hbar’, ‘vbar’, ‘column’, ‘hist’, ‘histogram’.

subtype

  • bar/column: c(‘stack’)
    • stack: stacked series
  • hist: c(‘count’, ‘freq’, ‘density’)
    • count/freq: stat by freq
    • denstiy: stat by density

3 Showcase

3.1 Data Preparation

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

3.2 Horizontal Bar Chart

3.2.1 Singular Series

type could be ‘hbar’, ‘bar’ or ‘auto’.

echartr(titanic[titanic$Survived=='Yes',], Class, Count) %>%
    setTitle('Titanic: N Survival by Cabin Class')

3.2.2 Multiple Series

echartr(titanic, Class, Count, Survived) %>%
    setTitle('Titanic: Survival Outcome by Cabin Class')

3.2.3 Stacked Horizontal Bar Chart

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') 

3.2.4 Tornado Chart

Tornado chart is a special case. The keys are:

  • An all-positive series and an all-negative series
  • Tiled rather than stacked
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);}')
    ))

3.2.5 Population Pyramid

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);}')
    ))

3.2.6 Bar Chart with Timeline

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")

3.3 Vertical Bar (Column) Chart

3.3.1 Tiled Vertical Bar (Column) Chart

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') 

3.3.2 Stacked Vertical Bar (Column) Chart

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') 

3.4 Histogram

3.4.1 Stat by Frequency

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)

3.4.2 Stat by Density

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)

4 Futher Setup

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.