Good morning, colleagues! Now I try to build the graph - Japanese candles through the ggplot package in R, but the code doesn't want to work. The error is:
Discrete value supplied to continuous scale.
I suggest that the data are not shown as numeric for ggplot but if I change in the code: as.vector --> as.numeric, the problem does not disappear. Could you say me what I do wrong. Thanks.
library("dplyr")
library("ggplot2")
library("quantmod")
getSymbols('AAPL')
x<-AAPL
head(x)
start=as.Date("2017-01-01")
end=as.Date("2017-09-01")
candle <- function(x, start, end){
date <- as.Date(time(x))
open <- as.vector(Op(x))
high <- as.vector(Hi(x))
low <- as.vector(Lo(x))
close <- as.vector(Cl(x))
xSubset <-data.frame('date'=date,'open'=open,'high'= high,'low'=low,'close'=close)
xSubset$candleLower <- pmin(xSubset$open, xSubset$close)
xSubset$candleMiddle <- NA
xSubset$candleUpper <- pmax(xSubset$open, xSubset$close)
xSubset$fill <- ''
xSubset$fill[xSubset$open < xSubset$close] = 'white'
xSubset$fill[xSubset$fill ==''] = 'red'
xSubset$ma200 <- SMA(xSubset$close, 200)
xSubset$ma50 <- SMA(xSubset$close, 50)
xSubset <-subset(xSubset, xSubset$date > start & xSubset$date < end)
g <- ggplot(xSubset, aes(x=date, lower=candleLower, middle=candleMiddle, upper=candleUpper, ymin=low, ymax=high))
g <- g + geom_boxplot(stat='identity', aes(group=date, fill=fill))
g <- g + geom_line(aes(x=date, y=ma50))+ geom_line(aes(x=date, y=ma200))
g
}
candle(AAPL, start, end)