First, you should load recharts:

library(recharts)

1 Introduction

Pie plot includes 3 basic types:

  • Pie: pie
  • Ring: ring
  • Nightingale rose chart: rose

The keys are:

  • character x and numeric y and will be compacted using data.table::dcast with fun=sum
  • x is used as series, series is used to produce multi-tier pies/rings, while facet is used to produce separate pies!
  • Pie and funnel charts can shift from one to another by clicking type shift buttons in the toolbox widget.

2 Function Call

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 x is treated as a data series. Other type will be coerced to factors. Only the first one is accepted if multiple variables are provided.

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 series is treated as a tyre factor to produce rings towards the center. Only the first one is accepted if multiple variables are provided.

facet

facetting variable which will be coerced to factors. Each level of facet is treated as a subsetting factor to produce separate pies. 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

‘pie’, ‘ring’, ‘rose’.

subtype

  • pie: c(“multi”,“clock”,“clockwise”)
    • multi: multiple select mode
    • clock/clokwise: pie/ring display clockwise
  • ring: c(“info”,“multi”,“clock”,“clockwise”)
    • info: infographic ring
  • rose: c(“area”,“radius”,“multi”,“clock”,“clockwise”)
    • area: rose chart in area mode
    • radius: rose chart in radius mode

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 Pie Chart

3.2.1 Single Pie

type is set ‘pie’.

echartr(titanic, Class, Count, type='pie') %>%
    setTitle('Titanic: N by Cabin Class')

3.2.2 Multiple Pies

Pie chart uses facet as subsetting factor to produce seperate polar systems. So when we apply Class (containing 4 levels) as facet, we get 4 pies.

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

3.2.3 Multiple Series without X

What if we only provide facet and y? We get several dichotomous pies.

echartr(titanic, y=Count, facet=Class, type='pie') %>%
    setTitle('Titanic: Propotion of Each Cabin Class')

3.2.4 Multi-ring Pie Charts

If series is given, you will yield a multi-ring pie chart. type and subtype are organized as vectors in association with levels of series.

echartr(titanic, Survived, Count, Class, type=c(rep('ring', 3), 'pie'),
        subtype='clock') %>% setTitle('Titanic: Survival Outcome by Cabin Class')

You can use series and facet to produce multiple multi-tier pies.

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, Survived, Count, Class, facet=Sex, 
        type=c(rep('ring', 3), 'pie'), subtype='clock') %>% 
    setTitle('Titanic: Cabin-specific Survival Outcome by Sex')

3.2.5 Pies With Timeline

We need another variable as timeline. Let’s say, ‘sex’.

echartr(titanic_sex, Survived, Count, facet=Class, t=Sex, type='pie') %>% 
    setTitle("Titanic: Survival Outcome by Cabin Class Across Sex") %>%
    setPolar(radius='30%')

3.3 Ring Chart

3.3.1 Regular Ring Chart

type is set ‘ring’.

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

3.3.2 Infographic Ring Chart

Infographic ring chart is a special type. We need to carefully configure the aesthetic parameters step by step.

ds <- data.frame(q=c('68% feel good', '29% feel bad', '3% have no feelings'),
              a=c(68, 29, 3))

Build the base chart.

g <- echartr(ds, q, a, type='ring', subtype='info') %>% 
    setTheme('macarons', width=800, height=600) %>%
    setTitle('How do you feel?','ring_info', 
             pos=c('center','center', 'horizontal'))
g

But the legend is not at the right position. So we first set its pos=c('center','top','vertical'), then tune the location using relocLegend.

echartr uses tuneGrid function to adjust the sizing, positioning of all the widgets, so if you set the exact sizing and positionning paramemters using setLegend, it will be overrided. relocWidget is always recommended to be used at the end of the pipe chain call.

width = 800
height = 600
g %>% setLegend(pos=c('center','top','vertical'), itemGap=height/25) %>%
    relocLegend(x=width/2, y=height/8)

3.4 Nighingale Rose Chart

3.4.1 Radius Mode

type is set ‘rose’, subtype is set ‘radius’.

echartr(titanic_sex, Class, Count, facet=Survived, type='rose', 
        subtype='radius') %>%
    setTitle('Titanic: Survival Outcome by Cabin Class') 

3.5 Area Mode

type is set ‘rose’, subtype is set ‘area’.

echartr(titanic_sex, Class, Count, facet=Survived, type='rose', 
        subtype='area') %>%
    setTitle('Titanic: Survival Outcome by Cabin Class') 

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.