Home > Enterprise >  How to plot with R
How to plot with R

Time:12-27

I would like to do candlestick plot with R and also add the donchian channel as indicator line. In the example below, the dataframe is not being extracted. not sure where i am making a error.

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

CodePudding user response:

plot_ly want a data frame, but

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

so it says Error: First argument, data, must be a data frame or shared data.
You need to convert class of s, below is an example;

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) 

CodePudding user response:

You get the error because plot_ly() expects the data to be a dataframe, and your data is not.

If you do class(s) you'll see that the object that is returned from getSymbols() has the classes xts and zoo, but not data.frame.

You can make the plot work by by transforming s to a dataframe before plotting. E.g. with dplyr::as_tibble(s, rownames = "Date"). Under is a full example based on your code.

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")
  • Related