First, you should load recharts:

library(recharts)

1 Introduction

Line plot includes 4 basic types:

  • line: line
  • curve (smooth line): curve
  • area: area
  • wave (smooth area): wave

The keys are:

  • character x and numeric y
  • each combination level of x (and series) can only have one y data point
  • Tiled and stacked bar/column charts can shift from one to another by clicking type shift buttons in the toolbox widget.
  • Line/area can shift to bar charts easily using buttons in toolbox widget.

2 Function Call

echartr(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

  • line: c(“stack”,“smooth”,“dotted”,“solid”,“dashed”)
    • stack: stacked series
    • smooth: smooth line
    • dotted: use dotted line
    • solid: use solid line (default)
    • dashed: use dashed line
  • curve: c(“stack”,“solid”)
  • area: c(“stack”,“smooth”,“dotted”,“solid”,“dashed”)
  • wave: c(“stack”,“solid”)

3 Showcase

3.1 Data Preparation

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

3.2 Line Chart

3.2.1 Singular Series

type is set ‘line’.

echartr(aq, Date, Temp, type='line') %>%
    setTitle('NY Temperature May - Sep 1973') %>% setSymbols('none')

3.2.2 Multiple Series

echartr(aq, Day, Temp, Month, type='line') %>%
    setTitle('NY Temperature May - Sep 1973, by Month') %>% 
    setSymbols('emptycircle')

3.2.3 Stacked Line Chart

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')

3.2.4 Line Width Mapped to 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')

3.2.5 Line Chart with Timeline

echartr(aq, Day, Temp, t=Month, type='line') %>%
    setTitle('NY Temperature May - Sep 1973, by Month') %>% 
    setSymbols('emptycircle')

3.3 Curve (Smooth Line) Chart

3.3.1 Tiled Smooth Line Chart

Set type ‘curve’.

echartr(aq, Day, Temp, Month, type='curve') %>%
    setTitle('NY Temperature May - Sep 1973, by Month') %>% 
    setSymbols('emptycircle')

3.3.2 Stacked Smooth Line Chart

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')

3.4 Area Chart

3.4.1 Tiled Area Chart

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')

3.4.2 Stacked Area Chart

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')

3.5 Wave (Smooth Area) Chart

3.5.1 Tiled Smooth Area Chart

Set type ‘wave’.

echartr(aq, Date, Temp, type='wave') %>%
    setTitle('NY Temperature May - Sep 1973') %>% 
    setSymbols('emptycircle')

3.5.2 Stacked Smooth Area Chart

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')

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.