为 plot.xts() 图例添加白色背景 [R]

Add White Background to plot.xts() Legend [R]

我希望使用 plot.xts()(R 中 xts 对象的内置包)绘制多个时间序列。

目前,我使用的是:

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

这会产生下图:

我想给图例添加一个白色的叠加背景,这样图例区域就看不到网格线了。这将使图形区域更清晰,并允许在类似出版物的文档中看到图例标签。

编辑

@JuliusVainora 给出的解决方案效果很好。简单解决我的问题。

您可以使用

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

其中 bty="o" 给出了一个白色背景的完整框。如果您希望边框颜色也为白色,请添加 box.col="white".