I'm working on R (version 4.4.2) I'm using the library echarts4r
(version 0.4.5) to create a plot. I want to use e_facet
to perform something like facet_wrap
in ggplot2
, splitting the plot into two columns by some group. However I'm not being able to do it with a funnel chart, but totally able to do it with other type like lines. I'll show an example below
library(echarts4r)
library(magrittr) # For the pipes
# I'll create an example data set
df <- data.frame(group = c('A','A','B','B'),
label = c('step1','step2','step1','step2'),
value = c(.99,.85,.97,.7))
this data set looks like this
> df
group label value
1 A step1 0.99
2 A step2 0.85
3 B step1 0.97
4 B step2 0.70
So, based on the documentation of e_facet
, we should previously group_by
the variable that we want to facet. This perfectly works with e_line()
.
df %>%
group_by(group) %>%
e_charts(label) %>%
e_line(value, label) %>%
e_facet()
But if I use e_facet
with a funnel (e_funnel
), it would facet the axis and both the funnels would be plotted in the center of the image, overlapping.
df %>%
group_by(group) %>%
e_charts(label) %>%
e_funnel(value, label) %>%
e_facet()
Adapting this solution for your usecase works here (group_split
by group var + imap
two plots into e_arrange
). This also works inside a R-Markdow
file. I've adjusted the values of group B to make the difference more apparent.
---
title: "echartsFunnel"
output: html_document
date: "2025-05-12"
---
```{r setup, include=FALSE}
library(echarts4r)
library(tidyverse)
# adapted example data to make the difference clear
df <- data.frame(group = c('A','A','B','B'),label = c('step1','step2','step1','step2'), value = c(.99,.85,.3,.1))
```
## Faceted Echarts4r Funnel Plot
```{r plot, echo=FALSE}
df %>%
group_split(group) %>%
imap(\(j, k) {
j %>%
e_charts(value, name = paste0("chart_", k)) %>%
e_funnel(value, labels = label, name = paste0("group ", k)) }) %>%
append(c(rows = 1, cols = 2)) %>%
do.call(e_arrange, .)
```
giving
Here you can use manipulateWidtgets::combineWidgets
to wrap the two funnel plots within one html-widget whilst specifying byrow = FALSE
so that the widgets are filled up in columns. Also one has to set the width to make the widget fill the whole slide. I used do.call()
to iterate over the funnel plots in t
and combine them all within combineWidgets
---
title: "echartsFunnel"
output:
xaringan::moon_reader:
nature:
ratio: '16:9'
date: "2025-05-12"
---
```{r setup, include=FALSE}
library(echarts4r)
library(tidyverse)
library(htmltools)
# adapted example data to make the difference clear
df <- data.frame(group = c('A','A','B','B'),label = c('step1','step2','step1','step2'), value = c(.99,.85,.3,.1))
```
## Faceted Echarts4r Funnel Plot
```{r plot, echo=FALSE}
t <- df %>%
group_split(group) %>%
imap(\(j, k) {
j %>%
e_charts(value, name = paste0("chart_", k)) %>%
e_funnel(value, labels = label, name = paste0("group ", k)) })
do.call(manipulateWidget::combineWidgets, c(t, list(byrow = FALSE, width = 1000))) # adjust width
```
giving