r - 如何从现有线的末尾开始绘制一条线?
问题描述
我想在现有线的末尾画三条虚线。这是我的数据集和代码:
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的点。
谁能帮我?
解决方案
这个解决方案一点也不漂亮,但你可以试试这个。从您的初始数据帧x
和future
,创建新的数据帧结合两个数据,并将数据帧切片以显示代码如下:
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()
适当的切片。
推荐阅读
- r - 在小标题上按组减去值
- java - Gson/Volley Android 从响应中获取消息而不是数据
- javascript - 在画布上双击删除点
- javascript - 如果字符串参数等于div的文本,如何隐藏最近的标签
- postgresql - 使用谷歌电子表格中的谷歌应用脚本连接到 postgresql 数据库
- java - 在 Rethink db 中,如何在以下情况下检索键值对?
- gimp - GIMP:将所有新图像设置为默认透明背景?
- c# - 如何在依赖注入期间初始化字典类型
- maven - Tycho 复制依赖不包括插件依赖
- imagesharp - 使用 ImageSharp 加载和保存不透明的 8 位 PNG 文件