【问题标题】:What color does plot.xts use?plot.xts 使用什么颜色?
【发布时间】:2021-08-25 06:29:51
【问题描述】:

有人知道 plot.xts 使用什么颜色吗?我在帮助页面上找不到任何东西。 我想在我的图例中使用相同的颜色。

或者还有其他方法可以使用 addLegend() 获得相同的情节吗?

这里是我使用的代码:

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)

【问题讨论】:

    标签: r plot xts


    【解决方案1】:

    回答

    使用chart.CumReturnscolorset 参数:

    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))
    

    【讨论】:

      猜你喜欢
      • 2012-02-19
      • 2013-08-29
      • 1970-01-01
      • 2016-05-08
      • 1970-01-01
      • 2020-06-20
      • 1970-01-01
      • 1970-01-01
      • 2012-07-17
      相关资源
      最近更新 更多