如何用 R 绘图

How to plot with R

我想用 R 绘制烛台图,并将 donchian 通道添加为指示线。在下面的示例中,未提取数据框。不确定我哪里出错了。

library(reprex)
library(quantmod)
#> Loading required package: xts
#> Loading required package: zoo
#> 
#> Attaching package: 'zoo'
#> The following objects are masked from 'package:base':
#> 
#>     as.Date, as.Date.numeric
#> Loading required package: TTR
#> Registered S3 method overwritten by 'quantmod':
#>   method            from
#>   as.zoo.data.frame zoo
library(PerformanceAnalytics)
#> 
#> Attaching package: 'PerformanceAnalytics'
#> The following object is masked from 'package:graphics':
#> 
#>     legend
library(plotly)
#> Loading required package: ggplot2
#> 
#> Attaching package: 'plotly'
#> The following object is masked from 'package:ggplot2':
#> 
#>     last_plot
#> The following object is masked from 'package:stats':
#> 
#>     filter
#> The following object is masked from 'package:graphics':
#> 
#>     layout
s <- get(getSymbols('CWK'))["2019::"]
#> 'getSymbols' currently uses auto.assign=TRUE by default, but will
#> use auto.assign=FALSE in 0.5-0. You will still be able to use
#> 'loadSymbols' to automatically load data. getOption("getSymbols.env")
#> and getOption("getSymbols.auto.assign") will still be checked for
#> alternate defaults.
#> 
#> This message is shown once per session and may be disabled by setting 
#> options("getSymbols.warning4.0"=FALSE). See ?getSymbols for details.
s$sma20 <- SMA(Cl(s) , 20)
head(s,3)
#>            CWK.Open CWK.High CWK.Low CWK.Close CWK.Volume CWK.Adjusted sma20
#> 2019-01-02    14.29    14.75   13.82     14.53     911000        14.53    NA
#> 2019-01-03    14.51    14.70   13.97     14.19     627100        14.19    NA
#> 2019-01-04    14.25    14.35   13.60     14.25     697100        14.25    NA
s$dc <- DonchianChannel(s[,c("CWK.High","CWK.Low")],400,TRUE)
head(s,3)
#>            CWK.Open CWK.High CWK.Low CWK.Close CWK.Volume CWK.Adjusted sma20
#> 2019-01-02    14.29    14.75   13.82     14.53     911000        14.53    NA
#> 2019-01-03    14.51    14.70   13.97     14.19     627100        14.19    NA
#> 2019-01-04    14.25    14.35   13.60     14.25     697100        14.25    NA
#>            high mid dc
#> 2019-01-02   NA  NA NA
#> 2019-01-03   NA  NA NA
#> 2019-01-04   NA  NA NA

fig <- s %>% plot_ly(x = ~Date, type="candlestick",
          open = ~CWK.Open, close = ~CWK.Close,
          high = ~CWK.High, low = ~CWK.Low) 
#> Error: First argument, `data`, must be a data frame or shared data.
fig <- fig %>% layout(title = "Basic Candlestick Chart")
#> Error in layout(., title = "Basic Candlestick Chart"): object 'fig' not found

fig
#> Error in eval(expr, envir, enclos): object 'fig' not found

plot_ly 想要一个数据框,但是

> class(s)
[1] "xts" "zoo"

所以它说 Error: First argument, data, must be a data frame or shared data.
您需要转换 s 的 class,下面是一个例子;

fig <- tibble::as_tibble(s, rownames = "Date") %>% 
  plot_ly(x = ~Date, type="candlestick",
         open = ~CWK.Open, close = ~CWK.Close,
         high = ~CWK.High, low = ~CWK.Low) 

您收到错误是因为 plot_ly() 期望数据是数据帧,而您的数据不是。

如果你这样做 class(s) 你会看到从 getSymbols() 返回的对象有 类 xtszoo,但没有data.frame.

您可以通过在绘图前将 s 转换为数据框来使绘图工作。例如。 dplyr::as_tibble(s, rownames = "Date")。下面是基于您的代码的完整示例。

library(quantmod)
library(PerformanceAnalytics)
library(plotly)

s <- getSymbols('CWK', auto.assign = FALSE, from = "2019-01-01")

s$sma20 <- SMA(Cl(s) , 20)
s$dc <- DonchianChannel(s[ , c("CWK.High", "CWK.Low")], 400, TRUE)

s %>% 
  # Turn into a dataframe
  dplyr::as_tibble(rownames = "Date") %>% 
  plot_ly(
    x = ~Date, 
    type = "candlestick",
    open = ~CWK.Open, 
    close = ~CWK.Close,
    high = ~CWK.High, 
    low = ~CWK.Low
  ) %>% 
  layout(title = "Basic Candlestick Chart")