First, you should load recharts
:
library(recharts)
Line plot includes 4 basic types:
The keys are:
x
and numeric y
x
(and series
) can only have one y
data pointechartr(data, x, y, <series>, <weight>, <t>, <type>, <subtype>)
Arg | Requirement |
---|---|
data |
source data in the form of data.frame |
x |
character independent variable. 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 |
data series variable which will be coerced to factors. Only the first one is accepted if multiple variables are provided. |
weight |
weight is mapped to the lineWidth of the chart by series |
t |
timeline variable which will be coerced to factors. Only the first one is accepted if multiple variables are provided. |
type |
‘line’, ‘curve’, ‘area’, ‘wave’ |
subtype |
|
Let’s look into airquality
dataset embeded in the package datasets
. The structure of the data.frame is as below:
aq <- airquality
aq$Date <- as.Date(paste('1973', aq$Month, aq$Day, sep='-'))
aq$Day <- as.character(aq$Day)
aq$Month <- factor(aq$Month, labels=c("May", "Jun", "Jul", "Aug", "Sep"))
head(aq)
## Ozone Solar.R Wind Temp Month Day Date
## 1 41 190 7.4 67 May 1 1973-05-01
## 2 36 118 8.0 72 May 2 1973-05-02
## 3 12 149 12.6 74 May 3 1973-05-03
## 4 18 313 11.5 62 May 4 1973-05-04
## 5 NA NA 14.3 56 May 5 1973-05-05
## 6 28 NA 14.9 66 May 6 1973-05-06
type
is set ‘line’.
echartr(aq, Date, Temp, type='line') %>%
setTitle('NY Temperature May - Sep 1973') %>% setSymbols('none')
echartr(aq, Day, Temp, Month, type='line') %>%
setTitle('NY Temperature May - Sep 1973, by Month') %>%
setSymbols('emptycircle')
Set type
‘line’ and subtype
‘stack’.
echartr(aq, Day, Temp, Month, type='line', subtype='stack') %>%
setTitle('NY Temperature May - Sep 1973, by Month') %>%
setSymbols('emptycircle')
Weight
Let the lineWidth linked to the mean of Wind across series (Month).
echartr(aq, Day, Temp, Month, weight=Wind, type='line') %>%
setTitle('NY Temperature May - Sep 1973, by Month') %>%
setSymbols('emptycircle')
echartr(aq, Day, Temp, t=Month, type='line') %>%
setTitle('NY Temperature May - Sep 1973, by Month') %>%
setSymbols('emptycircle')
Set type
‘curve’.
echartr(aq, Day, Temp, Month, type='curve') %>%
setTitle('NY Temperature May - Sep 1973, by Month') %>%
setSymbols('emptycircle')
Set type
‘curve’ and subtype
‘stack’.
echartr(aq, Day, Temp, Month, type='curve', subtype='stack') %>%
setTitle('NY Temperature May - Sep 1973, by Month') %>%
setSymbols('emptycircle')
Area chart is exactly the same with line chart except that it has areaStyle
feature specialized.
Set type
‘area’.
echartr(aq, Date, Temp, type='area') %>%
setTitle('NY Temperature May - Sep 1973') %>%
setSymbols('emptycircle')
Set type
‘area’ and subtype
‘stack’.
echartr(aq, Day, Temp, Month, type='area', subtype='stack') %>%
setTitle('NY Temperature May - Sep 1973, by Month') %>%
setSymbols('emptycircle')
Set type
‘wave’.
echartr(aq, Date, Temp, type='wave') %>%
setTitle('NY Temperature May - Sep 1973') %>%
setSymbols('emptycircle')
Set type
‘wave’ and subtype
‘stack’.
echartr(aq, Day, Temp, Month, type='wave', subtype='stack') %>%
setTitle('NY Temperature May - Sep 1973, by Month') %>%
setSymbols('emptycircle')
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.