r - Change plot area background color -
i graph in r using our company colors. means background of charts should light blue, plotting region should white. searching answers , found drawing rect job (almost). plotting region white , graph not visible anymore. possible?
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()
the issue plot white rectangle after plotting data, therefore overwriting them. since plot.xts
doesn't have argument add
allow call after drawing rectangle, solution see modify 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 univariate series plotted") if (is.null(y)) xycoords <- xy.coords(.index(x), x[, 1]) } ###the next 3 lines modifications made 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, @ = xycoords$x, labels = false, col = "#bbbbbb", ...) axis(1, @ = 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) }
then script become:
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)
the error you're getting (error in axis(1, @ = xycoords$x, labels = false, col = "#bbbbbb", ...) : formal argument "col" matched multiple actual arguments.
) thrown previous script. has fact plot.xts
uses several time argument ...
, argument col
both valid axis
, plot
(or here in modified version, lines
). if want avoid it, see 2 solutions: either want axis of same color line , therefore have change line says:
... axis(1, @ = xycoords$x, labels = false, col = "#bbbbbb", ...) ...
into
... axis(1, @ = xycoords$x, labels = false, ...) ...
or want axis have color intended writer of original plot.xts
in case need differenciate color of lines , of axis.
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, ...)} ... }
and in actual call:
plot.xtsmodified(spy, lcol=graph_blue)
Comments
Post a Comment