Plotting an xts should be really easy, but I can't seem to get the xlab, and ylab to work..
ylab="Price"
xlab="Date"
plot(x = basket[, 1], lty="dotted", xlab = xlab, ylab = ylab, col = "mediumblue", lwd = 2)
title("Cumulative")
This will plot the figure, but without any labels on the x- and y-axis.
There must be an easy solution to this. The plot looks as it should besides that issue. I have tried with xts.plot, zoo.plot, xyplot but none seem to do the trick.
Data sample
structure(c(1, 1.01463414634146, 0.926829268292683, 0.970731707317073,
0.953658536585366, 1, 0.998263888888889, 1.01159722222222, 1.05076388888889,
1.05034722222222, 1, 1.00178890876565, 0.985688729874776, 1.04293381037567,
1.04651162790698, 1, 0.976675478152698, 0.990359197636448, 1.06515316436013,
1.04571606282071), class = c("xts", "zoo"), index = structure(c(946944000,
947030400, 947116800, 947203200, 947462400), tzone = "UTC", tclass = "Date"), .Dim = 5:4, .Dimnames = list(
NULL, c("new.close", "new.close.1", "new.close.2", "new.close.3"
)))
It is plot.zoo, not zoo.plot. These all work for me:
library(xts)
plot(as.zoo(basket[, 1]), xlab = "X", ylab = "Y")
plot.zoo(basket[, 1], xlab = "X", ylab = "Y")
library(lattice)
xyplot(basket[, 1], xlab = "X", ylab = "Y")
library(ggplot2)
autoplot(basket[, 1]) + xlab("X") + ylab("Y")
Note
basket <-
structure(c(1, 1.01463414634146, 0.926829268292683, 0.970731707317073,
0.953658536585366, 1, 0.998263888888889, 1.01159722222222, 1.05076388888889,
1.05034722222222, 1, 1.00178890876565, 0.985688729874776, 1.04293381037567,
1.04651162790698, 1, 0.976675478152698, 0.990359197636448, 1.06515316436013,
1.04571606282071), class = c("xts", "zoo"), index = structure(c(946944000,
947030400, 947116800, 947203200, 947462400), tzone = "UTC", tclass = "Date"), .Dim = 5:4, .Dimnames = list(
NULL, c("new.close", "new.close.1", "new.close.2", "new.close.3"
)))
I know this might not be exactly what you had in mind, but it gets the job done. Plus, you get to work with ggplot2 which way better than the standard plotting system. In addition, I am using ggfortify which extends ggplot() capabilities to handle time series.
library(xts)
library(zoo)
library(ggfortify)
library(ggplot2)
myts = structure(
c(1, 1.01463414634146, 0.926829268292683, 0.970731707317073,
0.953658536585366, 1, 0.998263888888889, 1.01159722222222, 1.05076388888889,
1.05034722222222, 1, 1.00178890876565, 0.985688729874776, 1.04293381037567,
1.04651162790698, 1, 0.976675478152698, 0.990359197636448, 1.06515316436013,
1.04571606282071),
class = c("xts", "zoo"),
index = structure(c(946944000, 947030400,
947116800, 947203200,
947462400),
tzone = "UTC",
tclass = "Date"),
.Dim = 5:4, .Dimnames = list(NULL,
c("new.close",
"new.close.1",
"new.close.2",
"new.close.3" ) )
)
autoplot( myts[ , 1], xlab = "Date", ylab = "Price" )
Created on 2020-05-05 by the reprex package (v0.3.0)
Related
The XTS is called 'x' and has the daily returns of seven currencies.
x=structure(c(0, 0, -0.00237747604278071, -4.10901316858503e-05,
-0.00292075554430804, -0.00327746821541597, 0, 0, -0.0024832242245989,
-7.13774881431206e-05, -0.00288771987280823, -0.00239072530655426,
0, 0, -0.00238446679144366, -0.00169293890925615, -0.0036441365731773,
-0.00370471543592221, 0, 0, -0.00244664834224606, 0, -0.00285354593582876,
-0.00288034192721653, 0, 0, -0.00310375411764707, -0.00146643546387215,
-0.00427725501167975, -0.00376793265740194, 0, 0, -0.00183281566844917,
0, -0.00294652695187159, -0.0021703202366018, 0, 0, -0.00243030417112311,
-0.000372071536924534, -0.00330455837816801, -0.00245257154873846
), class = c("xts", "zoo"), index = structure(c(1297296000, 1297382400,
1297641600, 1297728000, 1297814400, 1297900800), tzone = "UTC", tclass = "Date"), .Dim = 6:7, .Dimnames = list(
NULL, c("AUD", "EUR", "GBP", "JPY", "KRW", "TWD", "USD")))
I've used the PerformanceAnalytics function Drawdowns to calculate and plot the cumulative drawdown which is fine. However, I would also like a rolling 60 day drawdown and tried both rollapply and apply.rolling but no luck.
Can anyone please recommend a solution?
structure(c(0.00371570839572193, 0.00118836834224599, -0.00237747604278075,
0.00234195385026738, -0.00287978374331551, -0.000357757593582888,
0.00265543171122995, 0.000720424064171123, -0.00248322422459893,
0.00241785080213904, -0.00281654342245989, 0.000498433903743315,
0.00302572497326203, 0.00161579593582888, -0.00238446679144385,
0.000693180748663102, -0.00195450652406417, -6.08004278074866e-05,
0.00387943005347594, 0.00128238518716578, -0.00244664834224599,
0.00274308647058824, -0.00285354593582888, -2.68726737967914e-05,
0.00367270588235294, 0.00235053556149733, -0.00310375411764706,
0.00164241631016043, -0.00281494748663102, 0.000511510213903743,
0.00330757946524064, 0.00212843374331551, -0.0018328156684492,
0.00260363310160428, -0.00294652695187166, 0.000778500588235294,
0.00273476534759358, 0.000962438395721925, -0.00243030417112299,
0.00206324695187166, -0.00293357834224599, 0.000854811604278075
), class = c("xts", "zoo"), index = structure(c(1297296000, 1297382400,
1297641600, 1297728000, 1297814400, 1297900800), tzone = "UTC", tclass = "Date"), .Dim = 6:7, .Dimnames = list(
NULL, c("AUD", "EUR", "GBP", "JPY", "KRW", "TWD", "USD")))
rollapply(x, width = 60, FUN = Drawdowns)
Error in array(ans, c(len.a%/%d2, d.ans), if (!is.null(names(dn.ans)) || :
length of 'dimnames' [1] not equal to array extent
apply.rolling(x, width = 60, FUN = SharpeRatio)
Error in rbind(calcs, calc) :
number of columns of matrices must match (see arg 2)
here's a suggestion using the xts and PerformanceAnalytics packages:
I create a xts-object with simulated returns and apply the rollapply function from the zoo package (loaded with xts) with a width of 25 days.
tester
library(xts)
library(PerformanceAnalytics)
set.seed(123)
returns <- matrix(rnorm(n = 365*7, mean = 0.001, sd = 0.002), ncol = 7)
timeindex <- seq.Date(as.Date('2019-01-01'), to = as.Date('2019-12-31'), by = 'days')
test_xts <- xts(x = returns,
order.by = timeindex)
colnames(test_xts) <- c("AUD", "EUR", "GBP", "JPY", "KRW", "TWD", "USD")
rolling_maxDD <- rollapply(test_xts, width = 25, FUN = maxDrawdown)
plot.xts(-rolling_maxDD, legend.loc = 'bottomleft')
This is my code, it works so far, except that the legend does not appear in the plot:
plot(norm_prices_daily[,1], col = "blue",lwd=3,grid.col = NA, ylim = c(0,3))
lines(norm_prices_daily[,2],lwd=3, col = "dark green")
legend("bottomright",legend=c("Stock 1","Stock 2"),col = c("blue","dark green"))
norm_prices_daily is a zoo object with two columns and 182 rows.
Here is part of the data. Output from dput(head(norm_prices_daily[,1],20)):
structure(c(1, 0.9929401294387, 0.99644855697716, 0.9936552740678,
0.998526620668352, 1.00517207744161, 1.00230213170477, 1.00929440707496,
1.00776579745506, 1.0096505179963, 1.01809781273736, 1.02202987149226,
1.01931947115634, 1.01961413204347, 1.0207774879027, 1.01154745364837,
0.99563509229331, 1.0056447937894, 1.00477302623885, 1.00792234548049
), class = c("xts", "zoo"), .indexCLASS = "Date", .indexTZ = "UTC", tclass = "Date", tzone = "UTC", src = "yahoo", updated = structure(1600533381.35332, class = c("POSIXct",
"POSIXt")), index = structure(c(1577923200, 1578009600, 1578268800,
1578355200, 1578441600, 1578528000, 1578614400, 1578873600, 1578960000,
1579046400, 1579132800, 1579219200, 1579564800, 1579651200, 1579737600,
1579824000, 1580083200, 1580169600, 1580256000, 1580342400), tzone = "UTC", tclass = "Date"), .Dim = c(20L,
1L), .Dimnames = list(NULL, "^GSPC"))
I have tried different options for the position of the legend, such as specific x and y coordinates, because I suspected that the legend is there but just too low or something. But that does not seem to the issue.
You haven't specified the kind of line you want in the legend, but it should still print the labels. Here is a slight modification of the first example on the manual page for print.zoo to make it more comparable to yours:
x.Date <- as.Date(paste(2003, 02, c(1, 3, 7, 9, 14), sep = "-"))
set.seed(42)
x <- zoo(cbind(rnorm(5), rnorm(5, sd= 0.2)), x.Date)
plot(x[, 1], col = "blue")
lines(x[, 2], col = "dark green")
legend("bottomright", legend=c("Stock 1","Stock 2"), lty=1, col = c("blue","dark green"))
The following script plots 2 charts side by side:
require(xts)
par(mfrow=c(1,2))
XTS1 <- structure(c(12, 7, 7, 22, 24, 30, 26, 23, 27, 30), .indexCLASS = c("POSIXct", "POSIXt"), .indexTZ = "", tclass = c("POSIXct", "POSIXt"), tzone = "", class = c("xts", "zoo"), .CLASS = structure("double", class = "CLASS"), formattable = structure(list(formatter = "formatC", format = structure(list(format = "f", digits = 2), .Names = c("format", "digits")), preproc = "percent_preproc", postproc = "percent_postproc"), .Names = c("formatter", "format", "preproc", "postproc")), index = structure(c(1413981900, 1413982800, 1413983700, 1413984600, 1413985500, 1413986400, 1413987300, 1413988200, 1413989100, 1413990000), tzone = "", tclass = c("POSIXct", "POSIXt")), .Dim = c(10L, 1L))
XTS2 <- XTS1 ^ 0.2
plot(XTS1)
plot(XTS2)
The following script fails to plot 2 charts side by side:
require(PerformanceAnalytics)
require(xts)
par(mfrow=c(1,2))
XTS1 <- structure(c(12, 7, 7, 22, 24, 30, 26, 23, 27, 30), .indexCLASS = c("POSIXct", "POSIXt"), .indexTZ = "", tclass = c("POSIXct", "POSIXt"), tzone = "", class = c("xts", "zoo"), .CLASS = structure("double", class = "CLASS"), formattable = structure(list(formatter = "formatC", format = structure(list(format = "f", digits = 2), .Names = c("format", "digits")), preproc = "percent_preproc", postproc = "percent_postproc"), .Names = c("formatter", "format", "preproc", "postproc")), index = structure(c(1413981900, 1413982800, 1413983700, 1413984600, 1413985500, 1413986400, 1413987300, 1413988200, 1413989100, 1413990000), tzone = "", tclass = c("POSIXct", "POSIXt")), .Dim = c(10L, 1L))
XTS2 <- XTS1 ^ 0.2
charts.PerformanceSummary(XTS1)
charts.PerformanceSummary(XTS2)
Would anyone know how to get the latter script to plot 2 charts side by side?
I would like to avoid using another package if possible. Thanks.
chart.PerformanceSummary is really just a wrapper to several charts.
You could do this, and extend it to any number of symbols horizontally if you wish (more than 2 symbols if you wanted):
par(mfrow=c(3,2))
# First row
chart.CumReturns(XTS1, ylab = "Cumulative Return", main = "give me a title")
chart.CumReturns(XTS2, ylab = "Cumulative Return", main = "give me a title2")
# second row
chart.BarVaR(XTS1)
chart.BarVaR(XTS2)
# third row
chart.Drawdown(XTS1, main = "DD title", ylab = "Drawdown",
)
chart.Drawdown(XTS2, main = "", ylab = "Drawdown",
)
You need to add the appropriate parameters to each plot for things like colour and titles (leaving that to you), but you have the flexibility of adding any charts from the wonderful xts, quantmod, performanceAnalytics packages (and others).
I couldn't find any option to expand the margins for plot generated by corrplot.mixed in the corrplot package. Any suggestion would be appreciated!
library(corrplot)
cor_matrix <- structure(c(1, 0.31596392056465, -0.120092224085334, -0.345097115278159,
0.31596392056465, 1, 0.158912865564527, -0.606426850726639, -0.120092224085334,
0.158912865564527, 1, -0.134795548155303, -0.345097115278159,
-0.606426850726639, -0.134795548155303, 1), .Dim = c(4L, 4L),
.Dimnames = list(NULL, c("var_1", "var_2", "var_3", "var_4")))
corrplot.mixed(cor_matrix, order = "AOE", upper = "ellipse", lower = "number",
tl.cex = 2, cl.cex = 2, number.cex = 2)
Answer my own question for future readers
library(corrplot)
library(scico)
col4 <- scico(100, palette = 'vik')
filetag <- "corrplot_result.png"
png(filetag, height = 800, width = 800)
corrplot.mixed(cor_matrix, order = "AOE", upper = "ellipse", lower = "number",
upper.col = col4, lower.col = col4,
tl.cex = 2, cl.cex = 2, number.cex = 2)
dev.off()
x<- structure(list(count = c(4259120, 4317840, 4444000, 4254240,
4656800), the_date = structure(c(1389589200, 1389675600, 1389762000,
1389848400, 1389934800), class = c("POSIXct", "POSIXt"), tzone = "")), .Names = c("count",
"the_date"), row.names = c(51L, 406L, 664L, 197L, 196L), class = "data.frame")
par(mar = c(8, 4, 4, 2) + 0.1)
plot(x$the_date, x$count, type="l", xaxt = "n", xlab = "")
axis(1, labels = FALSE)
labels<-x$the_date
labels<-format(labels, format="%b-%d-%Y")
text(x$the_date, par("usr")[3] - 0.75, srt = 55, adj = 1, labels = labels, xpd = TRUE)
I've tried adjusting the par("usr")[3] - 0.75 offset as specified here, but the labels aren't moving at all.
You can a trick like this using 2 calls to axis functions. I am using here axis.Date since you deal with dates(better for formatting). Then you can ply with line argument t play with labels positions.
axis(1,labels=FALSE)
axis.Date(1,at = x$the_date,las=2, format= "%m-%d",line=0.5,tick=FALSE)