首页 > 解决方案 > 如何从现有线的末尾开始绘制一条线?

问题描述

我想在现有线的末尾画三条虚线。这是我的数据集和代码:

x = data.frame(Debt = c(115.413 , 116.522 , 123.361, 129.021, 131.786, 131.557, 131.397, 131.355, 132.1, 134.77))
future = data.frame(144.9, 147.9, 150.9)

plot(x$Debt, lwd = 2, lty = 1, type = "l", ylab = "", xlab ="", col = "red", xaxt = "n")
lines(????)
axis(1, at = seq(2010, 2020, 1), labels = seq(2010, 2020, 1))
abline(v = 2019, col = "black", ldy = 3)
legend("bottomright", col = c("black", "blue", "green"), bty = "n", lty = 1)

我想得到的情节的一个例子是:

在此处输入图像描述

在我的情况下,我想从x$Debt中绘制的线是future的点。

谁能帮我?

标签: rplot

解决方案


这个解决方案一点也不漂亮,但你可以试试这个。从您的初始数据帧xfuture,创建新的数据帧结合两个数据,并将数据帧切片以显示代码如下:

x <- data.frame(Debt = c(115.413 , 116.522 , 123.361, 129.021, 131.786, 131.557, 131.397, 131.355, 132.1, 134.77))
future <- data.frame(Debt=c(144.9, 147.9, 150.9))
df <- data.frame(x=c(1:(nrow(x)+nrow(future))), y= c(x$Debt, future$Debt))

plot(range(df[,1]), range(100,150), type='n')
lines(df[1:nrow(x),1], df[1:nrow(x),2], type='l', col='black')
lines(df[nrow(x):nrow(df),1], df[nrow(x):nrow(df),2], type='l', col='blue')

如果您想在未来范围内添加多行,您可以将带有年份和值的数据附加到df,并添加lines()适当的切片。


推荐阅读