早上好,同事们!我现在尝试使用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)
g <- g + coord_cartesian(ylim = c(50, 150))
? - Z.Lin