What color does plot.xts use?

Viewed 45

Does anybody know what colours plot.xts uses? I can't find anything on the help page. I would like to use the same colours in my legend.

Or is there another way to get the same plot with addLegend()?

Here the code I am using:

library(xts)
library(PerformanceAnalytics)
library(TTR)

xts1 <- xts(matrix(rnorm(300), ncol = 3), order.by = as.Date(1:100))
xts2 <- xts(matrix(rnorm(300), ncol = 3), order.by = as.Date(1:100))
colnames(xts1) <- c("A", "B", "C")
colnames(xts2) <- c("A", "B", "C")

plot_chart <- function(x) {
  ff <- tempfile()
  png(filename = ff)
  chart.CumReturns(x)
  dev.off()
  unlink(ff)
}

m <- matrix(c(1, 2, 3, 3), nrow = 2, ncol = 2, byrow = TRUE)
layout(mat = m, heights = c(0.8, 0.1))

par(mar = c(2, 2, 1, 1))
plot_chart(xts1)
addSeries(reclass(apply(xts1, 2, runSD), xts1))

par(mar = c(2, 2, 1, 1))
plot_chart(xts2)
addSeries(reclass(apply(xts2, 2, runSD), xts2))

par(mar=c(0, 0, 1, 0))
plot(1, type = "n", axes = FALSE, xlab = "", ylab = "")

# which colors do I have to insert in here?
plot_colors <- c("blue", "green", "pink")
legend(x = "top", inset = 0,
       legend = colnames(xts1), 
       col = plot_colors, lwd = 7, cex = .7, horiz = TRUE)
1 Answers

Answer

Use the colorset argument of chart.CumReturns:

plot_chart <- function(x, col) {
  ff <- tempfile()
  png(filename = ff)
  chart.CumReturns(x, colorset = col)
  dev.off()
  unlink(ff)
}

par(mar = c(2, 2, 1, 1))
plot_chart(xts1, col = plot_colors)
addSeries(reclass(apply(xts1, 2, runSD), xts1))

par(mar = c(2, 2, 1, 1))
plot_chart(xts2, col = plot_colors)
addSeries(reclass(apply(xts2, 2, runSD), xts2))

enter image description here

Related