简体   繁体   English

连续比例的离散值

[英]Discrete value to continuous scale

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. 现在我尝试构建图形 - 日本蜡烛通过R中的ggplot包,但代码不想工作。 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. 我建议数据不会显示为ggplot的数字,但如果我更改代码: as.vector - > as.numeric ,问题不会消失。 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)

Your problem is with the NA supplied for middle . 你的问题是为middle提供的NA It needs to be a continuous value. 它需要是一个连续的价值。 I changed it to 0s of the same length as candleUpper and candleLower and the error went away. 我把它改成了与candleUppercandleLower相同长度的0,错误就消失了。

Does that help? 这有帮助吗?

Have you tried using g + scale_y_continuous(limits = c(50, max(data))) to change the limits of your y-axis? 您是否尝试使用g + scale_y_continuous(limits = c(50,max(data)))来更改y轴的极限? That should work. 这应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM