【发布时间】:2018-05-02 01:46:27
【问题描述】:
早上好,同事们!现在我尝试通过 R 中的 ggplot 包构建图表 - 日本蜡烛,但代码不想工作。错误是:
提供给连续刻度的离散值。
我建议不要将数据显示为 ggplot 的数字,但如果我更改代码:as.vector --> as.numeric,问题不会消失。你能告诉我我做错了什么吗?谢谢。
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)
【问题讨论】: