First, you should load recharts:

library(recharts)

1 Introduction

Candlestick plot is also known as K chart:

The keys are:

  • character x
  • numeric ‘y’ (four columns) aranged in the order of ‘open’, ‘close’, ‘low’, ‘high’

2 Function Call

echartr(data, x, y, <t>, <type>)
Arg Requirement

data

source data in the form of data.frame

x

character independent variable. Only the first one is accepted if multiple variables are provided.

y

numeric dependent variable. Only the first four ones are accepted which are arranged in the order of ‘open’, ‘close’, ‘low’, ‘high’.

t

timeline variable which will be coerced to factors. Only the first one is accepted if multiple variables are provided.

type

‘k’ or ‘candlestick’

3 Showcase

3.1 Basic Plot

echartr(stock, as.character(date), c(open, close, low, high), type='k') %>%
    setXAxis(name='Date', axisLabel=list(rotate=30)) %>%
    setYAxis(name="Price")

3.2 With Timeline

Remember the special notes in the quick start? We need to supplement the dataset to cover all the x and t levels.

stock$Month <- format(stock$date, '%m')
stock$Day <- format(stock$date, '%d')
fullData <- data.frame(expand.grid(unique(stock$Month), unique(stock$Day)))
names(fullData) <- c("Month", "Day")
stock <- merge(stock, fullData, all.y=TRUE)

Then timeline will work properly.

echartr(stock, Day, c(open, close, low, high), t=Month, type='k') %>%
    setYAxis(name="Price")

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.