【问题标题】:Discrete value to continuous scale离散值到连续规模
【发布时间】: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)

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    您的问题在于为middle 提供的NA。它必须是一个连续的值。我将其更改为与candleUppercandleLower 长度相同的0,错误消失了。

    这有帮助吗?

    【讨论】:

    • 好的,谢谢!
    • 但是现在出现了第二个问题:图表中的蜡烛值高于100 y.e。 (在 y 刻度上)我想限制这个图表并显示以 50 或 100 开头的刻度(但不是 0)。我尝试添加 ggplot(ylim=c(50,150)),但对 obrained 图没有任何影响。
    • 你能更新你的新代码,甚至提出一个新问题吗?
    • @RuslanSayakhov 试试g &lt;- g + coord_cartesian(ylim = c(50, 150))?
    【解决方案2】:

    您是否尝试过使用 g + scale_y_continuous(limits = c(50, max(data))) 来更改 y 轴的限制?应该可以的。

    【讨论】:

      猜你喜欢
      • 2022-01-16
      • 2022-07-07
      • 2015-09-26
      • 2020-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多