First, you should load recharts
:
library(recharts)
Pie plot includes 3 basic types:
The keys are:
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!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 |
‘pie’, ‘ring’, ‘rose’. |
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
is set ‘pie’.
echartr(titanic, Class, Count, type='pie') %>%
setTitle('Titanic: N by Cabin Class')
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')
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')
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')
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%')
type
is set ‘ring’.
echartr(titanic, Survived, Count, facet=Class, type='ring') %>%
setTitle('Titanic: Survival Outcome by Cabin Class')
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
usestuneGrid
function to adjust the sizing, positioning of all the widgets, so if you set the exact sizing and positionning paramemters usingsetLegend
, 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)
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')
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')
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.