R图中的线应在其他时间点开始 [英] Line in R plot should start at a different timepoint
问题描述
我有以下示例数据集:
date<-c(1,2,3,4,5,6,7,8)
valuex<-c(2,1,2,1,2,3,4,2)
valuey<-c(2,3,4,5,6)
现在我绘制日期和valuex变量:
now I plot the date and the valuex variable:
plot(date,valuex,type="l")
现在,我想添加一行有价值的变量,但是它应该从第4天开始,所以不要在开始时,因此我要添加NA值:
now, I want to add a line of the valuey variable, but it should start with the 4th day, so not at the beginning, therefore I add NA values:
valuexmod<-c(rep(NA,3),valuex)
,然后添加以下行:
lines(date,valuexmod,type="l",col="red")
但这不起作用吗? R忽略了NA值,而valuexmod行从第一天开始,但是应该从第四天开始?
But this does not work? R ignores the NA values and the valuexmod line starts with the first day, but it should start with th 4th day?
推荐答案
给出 date
和 valuex
的长度相同,我假设您上面有一个错字。
Given that date
and valuex
have the same length, I am assuming that you have a typo above.
尝试以下操作:
date <- c(1, 2, 3, 4, 5, 6, 7, 8)
valuex <- c(2, 1, 2, 1, 2, 3, 4, 2)
valuey <- c(2, 3, 4, 5, 6)
valueymod <- c(rep(NA, 3), valuey)
plot(date, valuex, type = "l", ylim = range(c(valuex, valuey)))
lines(date, valueymod, type = "l", col = "red")
这是结果图:
与您的问题有关的是help( lines)...
Related to your question is a point made in help("lines")...
坐标可以包含NA值。如果一个点的x或y值都包含NA,则将其从图中省略,并且不会在此类点上绘制线或从此类点绘制线。因此,缺失值可用于实现换行。
The coordinates can contain NA values. If a point contains NA in either its x or y value, it is omitted from the plot, and lines are not drawn to or from such points. Thus missing values can be used to achieve breaks in lines.
这篇关于R图中的线应在其他时间点开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!