First, you should load recharts
:
library(recharts)
Candlestick plot is also known as K chart:
The keys are:
x
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’ |
echartr(stock, as.character(date), c(open, close, low, high), type='k') %>%
setXAxis(name='Date', axisLabel=list(rotate=30)) %>%
setYAxis(name="Price")
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")
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.