Problems to plot graph with function chartSeries of the quantmod package from a data.frame in R

4

I'm having trouble plotting a candlestick chart from a data.frame file.

Code sample

# Pacotes necessários ---------------------------------------------- 
library(BatchGetSymbols)
library(quantmod)
# Inputs necessários ------------------------------------------------------
#data inicial
first.date <- as.Date("2018-01-01")
#data final
last.date <- Sys.Date()
#frequencia das observações
freq.data <- 'daily'
# Ativos a serem baixados
tickers <- c("^BVSP")
# Importando ativos -------------------------------------------------------
ibov <- BatchGetSymbols(tickers = tickers, 
                      first.date = first.date,
                      last.date = last.date, 
                      freq.data = freq.data,
                      cache.folder = file.path(tempdir(), 
                                               'BGS_Cache') ) 
ibov<-as.data.frame(ibov)

This code imports the data for an example. What I would like to learn would be how to plot this data using the function.

chartSeries

From data.frame ibov , I tried to do this:

ibov <- xts(ibov, order.by = ibov$df.tickers.ref.date)
chartSeries(ibov)

But the error is appearing:

Error in (function (cl, name, valueClass)  : 
assignment of an object of class “character” is not valid for @‘yrange’ in 
an object of class “chob”; is(value, "numeric") is not TRUE
    
asked by anonymous 13.12.2018 / 17:23

1 answer

4

It worked for me:

library(tidyverse)
ibov <- xts(
  ibov$df.tickers %>% select(-ref.date, -ticker), 
  order.by = ibov$df.tickers$ref.date
  )
chartSeries(ibov)

I also deleted this line from your code: ibov<-as.data.frame(ibov) .

The idea is that at the time of converting to an object of type xts there can be no variable of type character in data.frame.

Another solution, given that you are already with the transformed data.frame would do so:

ibov <- xts(ibov %>% select_if(is.numeric), order.by = ibov$df.tickers.ref.date)
chartSeries(ibov)
    
27.12.2018 / 12:14