I am currently using the quantmod package in R. I'm trying to get the scatter plot for returns of x against returns of y, however whenever I use the plot function, it shows me a timeseries instead of a scatterplot.
install.packages('quantmod')
library('quantmod')
# set date
from.dat <- as.Date('01/01/2020', format='%d/%m/%y')
to.dat <- as.Date('31/12/2020', format='%d/%m/%y')
# extract data for SPY and VXX
getSymbols(c('SPY', 'VXX'), from=from.dat, to=to.dat)
# calculate returns
spy <- SPY$SPY.Close
vxx <- VXX$VXX.Close
spy_returns <- diff(log(spy))
vxx_returns <- diff(log(vxx))
# create a matrix containing both SPY and VXX returns
matrix <- merge(spy_returns, vxx_returns)
plot(spy_returns, vxx_returns)
I've checked the class of (spy_returns, vxx_returns), they are both xts zoo objects.
To create a scatterplot your data needs to be in vector form. So you need to transform your data from a timeseries to a vector. You can get the data out of a timeseries with the coredata
function from the zoo package. This package is loaded as soon as you load quantmod or xts.
The line of code below will return a scatterplot that you are looking for.
plot(coredata(spy_returns), coredata(vxx_returns)