4

我想用我们公司的颜色在 R 中做一个图表。这意味着所有图表的背景应该是浅蓝色,但是绘图区域应该是白色的。我正在寻找答案,发现画一个矩形(几乎)可以完成这项工作。然而,绘图区域现在是白色的,图表不再可见。这甚至可能吗?

getSymbols('SPY', from='1998-01-01', to='2011-07-31', adjust=T)

GRAPH_BLUE<-rgb(43/255, 71/255,153/255)
GRAPH_ORANGE<-rgb(243/255, 112/255, 33/255)
GRAPH_BACKGROUND<-rgb(180/255, 226/255, 244/255)

par(bg=GRAPH_BACKGROUND)

colorPlottingBackground<-function(PlottingBackgroundColor = "white"){
  rect(par("usr")[1], par("usr")[3], par("usr")[2], par("usr")[4], col ="white")
}

plot.xts(SPY, col=GRAPH_BLUE)
colorPlottingBackground()
4

3 回答 3

14

我知道您已经接受了@plannapus 的回答,但这是一个更简单的解决方案

par(bg="lightblue")
plot(0, 0, type="n", ann=FALSE, axes=FALSE)
u <- par("usr") # The coordinates of the plot area
rect(u[1], u[3], u[2], u[4], col="white", border=NA)

par(new=TRUE)
plot(1:10, cumsum(rnorm(10)))

您基本上要做的是使用以下方法覆盖两个图par(new=TRUE):一个只有一个白色矩形;另一个包含您实际想要绘制的内容。

在此处输入图像描述

于 2013-10-07T08:37:54.600 回答
4

问题是您在绘制数据后绘制了白色矩形,因此覆盖了它们。由于plot.xts没有add允许您在绘制矩形后调用它的参数,我看到的唯一解决方案是修改 function plot.xts

plot.xtsMODIFIED<-function (x, y = NULL, type = "l", auto.grid = TRUE, major.ticks = "auto", 
    minor.ticks = TRUE, major.format = TRUE, bar.col = "grey", 
    candle.col = "white", ann = TRUE, axes = TRUE, ...) 
{
    series.title <- deparse(substitute(x))
    ep <- axTicksByTime(x, major.ticks, format.labels = major.format)
    otype <- type
    if (is.OHLC(x) && type %in% c("candles", "bars")) {
        x <- x[, has.OHLC(x, TRUE)]
        xycoords <- list(x = .index(x), y = seq(min(x), max(x), 
            length.out = NROW(x)))
        type <- "n"
    }
    else {
        if (NCOL(x) > 1) 
            warning("only the univariate series will be plotted")
        if (is.null(y)) 
            xycoords <- xy.coords(.index(x), x[, 1])
    }
    ###The next three lines are the only modifications i made to the function####
    plot(xycoords$x, xycoords$y, type = "n", axes = FALSE, ann = FALSE) 
    rect(par("usr")[1], par("usr")[3], par("usr")[2], par("usr")[4], col ="white")
    if(type=="l"){lines(xycoords$x, xycoords$y, ...)}

    if (auto.grid) {
        abline(v = xycoords$x[ep], col = "grey", lty = 4)
        grid(NA, NULL)
    }
    if (is.OHLC(x) && otype == "candles") 
        plot.ohlc.candles(x, bar.col = bar.col, candle.col = candle.col, 
            ...)
    dots <- list(...)
    if (axes) {
        if (minor.ticks) 
            axis(1, at = xycoords$x, labels = FALSE, col = "#BBBBBB", 
                ...)
        axis(1, at = xycoords$x[ep], labels = names(ep), las = 1, 
            lwd = 1, mgp = c(3, 2, 0), ...)
        axis(2, ...)
    }
    box()
    if (!"main" %in% names(dots)) 
        title(main = series.title)
    do.call("title", list(...))
    assign(".plot.xts", recordPlot(), .GlobalEnv)
}

然后你的脚本变成:

library(quantmod)
getSymbols('SPY', from='1998-01-01', to='2011-07-31', adjust=T)

GRAPH_BLUE<-rgb(43/255, 71/255,153/255)
GRAPH_BACKGROUND<-rgb(180/255, 226/255, 244/255)

par(bg=GRAPH_BACKGROUND)

plot.xtsMODIFIED(SPY, col=GRAPH_BLUE)

在此处输入图像描述

您收到的错误 ( Error in axis(1, at = xycoords$x, labels = FALSE, col = "#BBBBBB", ...) : formal argument "col" matched by multiple actual arguments.) 也与您以前的脚本一起引发。plot.xts这与使用多个时间参数的事实有关,...并且该参数对andcol都有效(或在我的修改版本中,)。如果你想避免它,我看到了两种解决方案:要么你希望你的轴与你的线颜色相同,因此你必须改变这条线:axisplotlines

...
axis(1, at = xycoords$x, labels = FALSE, col = "#BBBBBB", 
            ...)
...

进入

...
axis(1, at = xycoords$x, labels = FALSE, ...)
...

或者您希望轴具有原作者想要的颜色,plot.xts在这种情况下,您需要区分线条的颜色和轴的颜色。

 plot.xtsMODIFIED<-function (x, y = NULL, type = "l", auto.grid = TRUE, major.ticks = "auto", 
                             minor.ticks = TRUE, major.format = TRUE, bar.col = "grey", 
                             candle.col = "white", ann = TRUE, axes = TRUE, 
                             lcol, ...) 
{
...
if(type=="l"){lines(xycoords$x, xycoords$y, lcol, ...)}
...
}

然后在您的实际通话中:

plot.xtsMODIFIED(SPY, lcol=GRAPH_BLUE)
于 2013-10-07T08:12:40.163 回答
1

plot.xts将接受panel.first参数,这是在绘制线条之前绘制矩形的另一种方法。

library(quantmod)
getSymbols('SPY', from='1998-01-01', to='2011-07-31', adjust=T)

GRAPH_BLUE<-rgb(43/255, 71/255,153/255)
GRAPH_BACKGROUND<-rgb(180/255, 226/255, 244/255)
par(bg=GRAPH_BACKGROUND)
white.rect=function() do.call(rect,as.list(c(par()$usr[c(1,3,2,4)],col="white")))
plot.xts(SPY,panel.first=white.rect())

这并没有解决col=GRAPH_BLUE@plannapus 指出的问题。

于 2013-10-08T12:30:24.347 回答