I am looking to plot several time series using plot.xts(), the built-in package for xts objects in R.
Currently, I am using the following:
## Create xts dataset
ust <- merge.xts(us.3m,
us.6m,
us.2y,
us.3y,
us.5y,
us.10y,
us.30y)
## Create color scheme using rainbow()
tsRainbow = rainbow(ncol(as.zoo(ust)))
## Create plot of dataset
plot.xts(ust, screens=1,
major.ticks="years",
main="U.S. Bond Yield Evolution",
yaxis.right=FALSE,
grid.ticks.on="years",
col=tsRainbow)
## Add legend
addLegend("topright",
legend.names=c("US 3M", "US 6M",
"US 2Y", "US 3Y",
"US 5Y", "US 10Y", "US 30Y"),
col=tsRainbow,
lty=c(1,1,1,1,1,1,1),
lwd=c(2,2,2,2,2,2,2),
ncol=2,
bg="white")
This produces the following graph:
I would like to add a white overlay background to the legend, so that the gridlines can not be seen in the legend area. This will make the graph area much cleaner, and allow the legend labels to be seen in a publication-like document.
The solution given by @JuliusVainora worked well. Simple fix to my problem.
You may use
addLegend("topright",
legend.names=c("US 3M", "US 6M",
"US 2Y", "US 3Y",
"US 5Y", "US 10Y", "US 30Y"),
col=tsRainbow,
lty=c(1,1,1,1,1,1,1),
lwd=c(2,2,2,2,2,2,2),
ncol=2,
bg="white",
bty="o")
where bty="o"
gives a complete box with a white background. If you wish the border color to be white as well, add box.col="white"
.