First, you should load recharts:
library(recharts)
Funnel plot includes 2 basic types:
The keys are:
x and numeric y and will be compacted using data.table::dcast with fun=sumx is used as series, series is used to produce stacked funnels, while facet is used to produce separate funnels!echartr(data, x, <y>, <series>, <facet>, <t>, <type>, <subtype>)
| Arg | Requirement |
|---|---|
data |
source data in the form of data.frame |
x |
character independent variable. Each level of |
y |
numeric dependent variable. Only the first one is accepted if multiple variables are provided. |
series |
series variable which will be coerced to factors. Each level of |
facet |
facetting variable which will be coerced to factors. Each level of |
t |
timeline variable which will be coerced to factors. Only the first one is accepted if multiple variables are provided. |
type |
‘funnel’, ‘pyramid’. |
subtype |
|
Let’s look into mtcars dataset embeded in the package datasets. The distribution by carb and am is shown as below:
cars <- data.table::dcast(mtcars, carb+am~., length, value.var='gear')
names(cars) <- c("carb", "am", "N")
knitr::kable(cars)
| carb | am | N |
|---|---|---|
| 1 | 0 | 3 |
| 1 | 1 | 4 |
| 2 | 0 | 6 |
| 2 | 1 | 4 |
| 3 | 0 | 3 |
| 4 | 0 | 7 |
| 4 | 1 | 3 |
| 6 | 1 | 1 |
| 8 | 1 | 1 |
type is set ‘funnel’.
echartr(cars, carb, N, type='funnel') %>%
setTitle('mtcars: carb distibution')
Set series to produce a stacked funnel chart.
echartr(cars, carb, N, am, type='funnel') %>%
setTitle('mtcars: carb distibution by am')
Funnel chart uses facet as subsetting factor to produce seperate polar systems. So when we apply am (containing 2 levels) as facet, we get 2 funnels.
echartr(cars, carb, N, facet=am, type='funnel') %>%
setTitle('mtcars: carb distibution by am')
We need another variable as timeline. Let’s say, ‘gear’.
cars_gear <- data.table::dcast(mtcars, carb+gear+am~., length, value.var='gear')
names(cars_gear) <- c("carb", "gear", "am", "N")
In order to make timeline work properly, we need an expand.grid dataset to cover all the combination of x and t levels.
fullData <- with(mtcars, data.frame(
expand.grid(unique(carb), unique(gear), unique(am))))
names(fullData) <- c("carb", "gear", "am")
cars_gear <- merge(cars_gear, fullData, all.y=TRUE)
echartr(cars_gear, carb, N, facet=am, t=gear, type='funnel') %>%
setTitle('mtcars: carb distibution by am across gear')
Left align.
echartr(cars, carb, N, facet=am, type='funnel', subtype='left') %>%
setTitle('mtcars: carb distibution by am across gear')
type is set ‘pyramid’.
echartr(cars, carb, N, facet=am, type='pyramid') %>%
setTitle('mtcars: carb distibution by am')
You can also mix funnel and pyramid. Note that you need to wrap type and subtype in list since they are thus associated with facets.
echartr(cars, carb, N, facet=am, type=list('funnel', 'pyramid')) %>%
setTitle('mtcars: carb distibution by am')
echartr(cars, carb, N, facet=am, type=list('funnel', 'pyramid'),
subtype=list('right', 'left')) %>%
setTitle('mtcars: carb distibution by am')
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.