Changes in Plotting an Xts Object

Change name in xts loop plots

Call ticker object function for main in the plot

for (i in 1:4){
print(plot.xts(x = basket[, i], xlab = "Time", ylab = "Cumulative Return",
major.ticks= "years", minor.ticks = FALSE,
col = "black", main = tickers[i]))
}

R - How can I change date format when I plot an xts & zoo object?

With xts, you can use major.format directly.

plot(price_AAPL, main = "The price of AAPL",major.format="%b-%d-%Y")

Sample Image

However, you should know that zoo plots are generally more flexible.

plot.zoo(price_AAPL, main = "The price of AAPL", xaxt="n", xlab="")
axis.Date(1,at=pretty(index(price_AAPL)),
labels=format(pretty(index(price_AAPL)),format="%b-%d-%Y"),
las=2, cex.axis=0.7)

Sample Image

Adjusting xaxis in plot.xts

You have just to subset your xts-object to zoom it:

xts_data <- AAPL[ , 6]
xts_zoom <- xts_data['2012/2015']
plot.xts(xts_zoom)

The reason why setting xlim manually does not work is that the xlim values are calculated inside the plot.xts() itself. See, for example, the rows 123-134 of the plot.xts() source code:

   if (cs$Env$observation.based) {
cs$Env$xycoords <- xy.coords(1:NROW(cs$Env$xdata[subset]))
cs$set_xlim(c(1, NROW(cs$Env$xdata[subset])))
cs$Env$xstep <- 1
}
else {
xycoords <- xy.coords(.index(cs$Env$xdata[cs$Env$xsubset]),
cs$Env$xdata[cs$Env$xsubset][, 1])
cs$Env$xycoords <- xycoords
cs$Env$xlim <- range(xycoords$x, na.rm = TRUE)
...
}

Another option is to use the built-in zoom tools of the quantmod package itself:

chartSeries(xts_data)
zoomChart('2012/2015')

R Why plot.xts creates extra graph after calling lines?

The xts plotting functions don't really work like base plotting functions despite they fact the calls look the same.

The plot.xts function returns an object. By default, if you don't assign the object anywhere, R will "print" the object which results in a plot being drawn. The lines.xts function changes the most recent plot object and adds a new series. Since the plot is not saved, that new object is printed as well. This new object remembers the first series as well as the newly added series.

The best thing to do would be to save these object and only print them when you are done adding layers. For example

par(mfrow = c(2,1))
pp <- plot(xts(x = 1:5, order.by = 1:5+as.Date("2017-01-01")), type="l", main = "plot.xts")
pp <- lines(xts(x = 5:1, order.by = 1:5+as.Date("2017-01-01")), main = "plot.xts")
pp #plot will be drawn here

Plot problem with xts-object containing different types

testXTS1 is an xts object, but it is filled with characters as the matrix also contains the date column. You can see that all the values are surrounded with quotes (") if you just print testXTS1 (See below). testXTS2 on the other hand is an xts as it is intended with the index being the dates and the matrix is filled with numbers. This is also the reason why your calculations don't work.

You need to exclude the date column from your data.frame when creating an xts object.

# printing objects to show difference:
testXTS1
Date A B
2014-12-31 "2014-12-31" "1" "1"
2015-01-03 "2015-01-03" "2" "3"
2015-01-04 "2015-01-04" "3" "5"

testXTS2
A B
2014-12-31 1 1
2015-01-03 2 3
2015-01-04 3 5

Error in plotting xts object: 'x' must be a time-series object

We need to select the Cases to avoid changing the class from numeric to character as xts is also a matrix and matrix can have only a single class

Bangladesh_xts <- xts( Bangladesh$Cases, order.by= as.Date(Bangladesh$Date))


Related Topics



Leave a reply



Submit