图例未绘制在图表中

Posted

技术标签:

【中文标题】图例未绘制在图表中【英文标题】:Legend not plotting in chart 【发布时间】:2021-10-26 22:09:12 【问题描述】:

我需要关于图例的帮助,当我运行代码时,它没有出现。

v_Treasury <- c("DGS5", "DGS10", "DGS30")
getSymbols(Symbols = v_Treasury,
src = "FRED")

我正在清理来自 NA 的数据集

US5Y<-DGS5["2021/2021"]
    US5Y<-na.omit(US5Y)
    US10Y<-DGS10["2021/2021"]
    US10Y<-na.omit(US10Y)
    US30Y<-DGS30["2021/2021"]
    US30Y<-na.omit(US30Y)

但情节在图中没有图例

plot(US5Y, lwd=2, col="blue",lty = "solid" ,ylab="", xlab="",ylim=c(0.4,3), 
main = "US Treasury Yields")
lines(US10Y, col="black", lwd=2, lty="dotted")
lines(US30Y, col="red", lwd=2, lty="dashed")
legend(x="topleft", legend=c("US5Y", "US10Y","US30Y"), col=c("blue", "black", "red"),
lwd=2, lty=c("solid", "dotted","dashed"))    

我想知道代码有什么问题,因为图例不在图中

【问题讨论】:

这很奇怪。 abline() 之类的函数也不起作用。不知道发生了什么... 【参考方案1】:

这是一个潜在的解决方案:

library(quantmod)

# Data from https://fred.stlouisfed.org/series/DGS5
DGS5 <- read.csv("DGS5.csv")
DGS10 <- read.csv("DGS10.csv")
DGS30 <- read.csv("DGS30.csv")

# the "getSymbols" function from quantmod specifies that
# this is not an 'ordinary' plot - it is an xts 'time-series' plot
# this has implications for e.g. the legend
v_Treasury <- c("DGS5", "DGS10", "DGS30")
getSymbols(Symbols = v_Treasury,
           src = "FRED")

US5Y<-DGS5["2021/2021"]
US5Y<-na.omit(US5Y)
US10Y<-DGS10["2021/2021"]
US10Y<-na.omit(US10Y)
US30Y<-DGS30["2021/2021"]
US30Y<-na.omit(US30Y)

# If you turn off clipping ("xpd=NA"), you can see where
# "legend()" is trying to put the legend ("topleft")
par(xpd=NA)
plot(US5Y, lwd=2, col="blue",lty = "solid" ,ylab="", xlab="",ylim=c(0.4,3), 
     main = "US Treasury Yields")
lines(US10Y, col="black", lwd=2, lty="dotted")
lines(US30Y, col="red", lwd=2, lty="dashed")
legend(x = "topleft", y = NULL, legend=c("US5Y", "US10Y","US30Y"),
       col=c("blue", "black", "red"),
       lwd=2, lty=c("solid", "dotted","dashed"))

# If you use the "addLegend()" function from the xts package
# then your legend goes where you want it to go
par(xpd=TRUE)
plot(US5Y, lwd=2, col="blue",lty = "solid" ,ylab="", xlab="",ylim=c(0.4,3), 
     main = "US Treasury Yields")
lines(US10Y, col="black", lwd=2, lty="dotted")
lines(US30Y, col="red", lwd=2, lty="dashed")
addLegend(legend.loc = "topleft", legend.names = c("US5Y", "US10Y","US30Y"),
          col=c("blue", "black", "red"), fill=c("blue", "black", "red"), ncol = 1)

【讨论】:

感谢您的帮助!!这对我很有帮助。 不客气;如果这解决了您的问题,请参阅What should I do when someone answers my question?【参考方案2】:

这是一个不同的解决方案(参见plot.xtslegend.loc 选项)。

library(quantmod)
v_Treasury <- c("DGS5", "DGS10", "DGS30")
getSymbols(Symbols = v_Treasury, src = "FRED")
US5Y <- DGS5["2021/2021"]
US5Y <- na.omit(US5Y)
US10Y <- DGS10["2021/2021"]
US10Y <- na.omit(US10Y)
US30Y <- DGS30["2021/2021"]
US30Y <- na.omit(US30Y)   

# Merge the 3 xts objects into one
USdat <- merge(US5Y, US10Y, US30Y, all=TRUE)  

# Add a legend using the legend.loc option of plot.xts
plot(USdat, lwd=2, 
     ylab="", xlab="",ylim=c(0.4,3), 
     col=c("blue", "black", "red"),
     lty=c("solid", "dotted","dashed"),
     main = "US Treasury Yields", legend.loc="topright")

【讨论】:

以上是关于图例未绘制在图表中的主要内容,如果未能解决你的问题,请参考以下文章

ggplot2 绘制两个图例

ggplot2:从列表和常见图例中在多个页面上绘制图形

Anychart 多个图例和标签

饼图图例颜色未更新 mpandroidchart

Py之matplotlib:matplotlib库图表绘制中常规设置大全(交互模式清除原有图像设置横坐标显示文字/旋转角度添加图例绘图布局自动调整图像显示图像暂停)

绘制无边框和白色背景的图例