在 R 中的线性回归图中的特定 x 和 y 值处插入线

Insert lines at specific x- and y-values in linear regression plot in R

嘿,我是 R 的新手,有一个问题

我有以下包含 12 种不同债券的数据集:

dput(Synthetic_bond_creation)
structure(list(`Days to maturity` = c(1419, 202, 1565, 1182, 
2080, 1036, 811, 2436, 1296, 609, 1792, 986), `Yield to maturity` = c(2.699, 
0.487, 4.019, 1.421, 2.394, 1.366, 1.107, 2.717, 1.592, 0.988, 
2.151, 2.278)), .Names = c("Days to maturity", "Yield to maturity"
), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-12L))

到目前为止我得到了以下信息:

library(readxl)
Synthetic_bond_creation <- read_excel("~/UZH/Bachelorarbeit/Synthetic bond 
creation.xlsx")
View(Synthetic_bond_creation)
plot(Synthetic_bond_creation$`Days to maturity`, 
Synthetic_bond_creation$`Yield to maturity`, xlab = "Days to maturity", ylab 
= "Yield to maturity in %", main = "Bonds of 'Societe Generale SA' on 
13.03.2013")
abline(lm (Synthetic_bond_creation$`Yield to maturity` ~ 
Synthetic_bond_creation$`Days to maturity`))

现在我想构建一个合成的 5 年期债券,这意味着我需要在 x=1300 的回归线上得到到期天数的值和相应的到期收益率 y 值,即1.951930573 四舍五入为 1.95。

我尝试使用:

abline(v=1300) +
abline(h=1.95)

但是我想调整线条,使它们在回归线处结束并且不超过它。此外,如果回归线截距处的 x 和 y 值出现在各自的轴上,那就太好了

我尝试 "draw" 我的目标如下所示:

您可以在 plot 和 abline 之后添加线段。

segments(x0=1300,y0=0,y1=1.95) #vertical line
segments(x0=0, y0=1.95, x1=1300) #horizontal line

起点 (x0, y0) 和终点 (x1, y1) 的默认值为 x,y,但由于它们是水平和垂直的,因此您只需分别更改 x 或 y。您可以照常更改线条类型和颜色,即 ltycol.

您可能需要调整两端的值,但您明白了。