First, you should load recharts
:
library(recharts)
Radar plot includes 1 basic types:
The keys are:
x
and numeric y
and will be compacted using data.table::dcast
with fun=sum
x
is used as indicators, while facet
is used to produce separate pies!setPolar
to tune the polar system parameters.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 |
‘radar’. |
subtype |
|
Let’s look into mtcars
dataset embeded in the package datasets
. We want to compare some paramters (c(‘mpg’,‘disp’,‘hp’,‘qsec’,‘wt’,‘drat’)) among several models (Mercedes-benz 450 series).
cars = mtcars[c('Merc 450SE','Merc 450SL','Merc 450SLC'),
c('mpg','disp','hp','qsec','wt','drat')]
cars$model <- rownames(cars)
cars <- data.table::melt(cars, id.vars='model')
names(cars) <- c('model', 'indicator', 'Parameter')
knitr::kable(cars)
model | indicator | Parameter |
---|---|---|
Merc 450SE | mpg | 16.40 |
Merc 450SL | mpg | 17.30 |
Merc 450SLC | mpg | 15.20 |
Merc 450SE | disp | 275.80 |
Merc 450SL | disp | 275.80 |
Merc 450SLC | disp | 275.80 |
Merc 450SE | hp | 180.00 |
Merc 450SL | hp | 180.00 |
Merc 450SLC | hp | 180.00 |
Merc 450SE | qsec | 17.40 |
Merc 450SL | qsec | 17.60 |
Merc 450SLC | qsec | 18.00 |
Merc 450SE | wt | 4.07 |
Merc 450SL | wt | 3.73 |
Merc 450SLC | wt | 3.78 |
Merc 450SE | drat | 3.07 |
Merc 450SL | drat | 3.07 |
Merc 450SLC | drat | 3.07 |
Set type
‘radar’ and do not set series
or facet
. But such a radar chart makes little sense.
echartr(cars, indicator, Parameter, type='radar')
So we always want to set data series to compare the parameters series by series.
echartr(cars, indicator, Parameter, model, type='radar', sub='fill') %>%
setTitle('Merc 450SE vs 450SL vs 450SLC')
If you don’t set series
, instead, set facet
, you will yield a bunch of radars shoulder by shoulder.
echartr(cars, indicator, Parameter, facet=model, type='radar') %>%
setTitle('Merc 450SE vs 450SL vs 450SLC')
You can set t
to yield a radar chart with timeline.
echartr(cars, indicator, Parameter, t=model, type='radar') %>%
setTitle('Merc 450SE vs 450SL vs 450SLC')
When it comes to timeline, you should pay special attention. In order to make timeline work properly, you need an expand.grid dataset to cover all the combinations of x, series and t
levels.
carstat = data.table::dcast(data.table::data.table(mtcars),
am + carb + gear ~., mean,
value.var=c('mpg','disp','hp','qsec','wt','drat'))
carstat = data.table::melt(carstat, id=c('am', 'carb', 'gear'))
names(carstat) <- c('am', 'carb', 'gear', 'indicator', 'Parameter')
levels(carstat$indicator) <- gsub("_mean_\\.", "",
levels(carstat$indicator))
carstat$am <- factor(carstat$am, labels=c('A', 'M'))
fullData <- data.frame(expand.grid(levels(carstat$indicator),
levels(carstat$am), unique(carstat$carb)))
carstat <- merge(fullData, carstat, all.x=TRUE)
carstat$carb <- as.factor(carstat$carb)
carstat$gear <- as.factor(carstat$gear)
str(carstat)
## 'data.frame': 5616 obs. of 8 variables:
## $ Var1 : Factor w/ 6 levels "mpg","disp","hp",..: 1 2 3 4 5 6 1 2 3 4 ...
## $ Var2 : Factor w/ 2 levels "A","M": 1 1 1 1 1 1 2 2 2 2 ...
## $ Var3 : num 1 1 1 1 1 1 1 1 1 1 ...
## $ am : Factor w/ 2 levels "A","M": 1 1 1 1 1 1 1 1 1 1 ...
## $ carb : Factor w/ 6 levels "1","2","3","4",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ gear : Factor w/ 3 levels "3","4","5": 1 1 1 1 1 1 1 1 1 1 ...
## $ indicator: Factor w/ 6 levels "mpg","disp","hp",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ Parameter: num 20.3 20.3 20.3 20.3 20.3 ...
echartr(carstat, indicator, Parameter, am, facet=carb, t=gear, type='radar') %>%
setTitle('Merc 450SE vs 450SL vs 450SLC')
setPolar
The default radar chart is built on polygons. You can built it on circle using setPolar
, which is only appliable for radar charts.
echartr(cars, indicator, Parameter, facet=model, type='radar') %>%
setTitle('Merc 450SE vs 450SL vs 450SLC') %>%
setPolar(type='circle')
You can explore setPolar
by typing ?setPolar
in R console.
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.