【问题标题】:Add White Background to plot.xts() Legend [R]将白色背景添加到 plot.xts() 图例 [R]
【发布时间】:2019-06-27 18:53:33
【问题描述】:

我希望使用 R 中 xts 对象的内置包 plot.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 给出的解决方案效果很好。简单解决我的问题。

【问题讨论】:

    标签: r plot xts


    【解决方案1】:

    你可以使用

    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"

    【讨论】:

      猜你喜欢
      • 2019-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多