首先,加载recharts
:
library(recharts)
饼图有3种基本类型:
关键是:
x
和数值型y
,会使用data.table::dcast
压缩(fun=sum
)x
用作数据系列,series
产生自外而内排布的圈层,facet
用于产生多个不同的饼图!echartr(data, x, <y>, <series>, <facet>, <t>, <type>, <subtype>)
参数 | 要求 |
---|---|
data |
数据框格式的源数据 |
x |
文本型自变量。 |
y |
数值型应变量。如提供多个变量,只传入第一个。 |
series |
转为因子后计算。 |
facet |
转为因子后计算。 |
t |
时间轴变量,转为因子后计算。如提供多个变量,只传入第一个。 |
type |
‘pie’, ‘ring’, ‘rose’. |
subtype |
|
考察datasets
包自带的数据集Titanic
。不同舱别生存人数如下:
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
设为’pie’。
echartr(titanic, Class, Count, type='pie') %>%
setTitle('Titanic: N by Cabin Class')
饼图用facet
作为区分不同极坐标系的分组变量。所以当我们把Class
(有4个水平) 作为facet
,可以得到4个饼图。
echartr(titanic, Survived, Count, facet=Class, type='pie') %>%
setTitle('Titanic: Survival Outcome by Cabin Class')
如果只提供facet
和y
会怎么样?我们会得到多个两分类饼图。
echartr(titanic, y=Count, facet=Class, type='pie') %>%
setTitle('Titanic: Propotion of Each Cabin Class')
如果提供series
,则生成多层饼图。type和subtype整理成vector,与series各水平映射。设为’ring’,则构成环环相套的多层饼图。
echartr(titanic, Survived, Count, Class, type=c(rep('ring', 3), 'pie'),
subtype='clock') %>% setTitle('Titanic: Survival Outcome by Cabin Class')
可以series和facet联用。
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')
需要一个时间轴变量。不妨就用’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
设为’ring’。
echartr(titanic, Survived, Count, facet=Class, type='ring') %>%
setTitle('Titanic: Survival Outcome by Cabin Class')
信息图样环图是特例。需要一步步小心地配置。
ds <- data.frame(q=c('68% feel good', '29% feel bad', '3% have no feelings'),
a=c(68, 29, 3))
构建底图。
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
但图例的位置不对。所以我们首先设置pos=c('center','top','vertical')
,然后用relocLegend
微调其位置。
每调用一次控件配置函数,
echartr
就会用tuneGrid
函数自动调整各控件的尺寸、位置。所以,如果你用setLegend
设置图例的尺寸和位置,会被后续的自动配置覆盖。建议把relocWidget
放在链式调用的末尾。
width = 800
height = 600
g %>% setLegend(pos=c('center','top','vertical'), itemGap=height/25) %>%
relocLegend(x=width/2, y=height/8)
type
设为’rose’,subtype
设为’radius’。
echartr(titanic_sex, Class, Count, facet=Survived, type='rose', subtype='radius') %>%
setTitle('Titanic: Survival Outcome by Cabin Class')
type
设为’rose’,subtype
设为’area’。
echartr(titanic_sex, Class, Count, facet=Survived, type='rose', subtype='area') %>%
setTitle('Titanic: Survival Outcome by Cabin Class')
接下来可以配置控件、添加标注点/标注线,以及美化成图。
参考相关函数,尽情探索吧。