R - Plotting a line with missing NA values

别等时光非礼了梦想. 提交于 2019-12-06 13:17:31

You could work with indices instead of na.omit(): something like this should do it:

plot(subset$Time[!is.na(subset$A)],subset$A[!is.na(subset$A)],type="l")  
# with your xlim and ylim, of course

lines(subset$Time,subset$B,type="p",col=27)
lines(subset$Time,subset$C,type="p",col=134)

Try this:

xlim <- range(subset$Time)
ylim <- range(subset[-1], na.rm = TRUE)
plot(A ~ Time, na.omit(subset[1:2]), type = "l", xlim = xlim, ylim = ylim)
points(B ~ Time, subset, col = 27)
points(C ~ Time, subset, col = 134)

Another possibility is to use the subset= argument of plot in which case we replace the plot line above with:

ok <- ! is.na(subset$A)
plot(A ~ Time, subset, subset = ok, type = "l", xlim = xlim, ylim = ylim)

In either case we get this graphic:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!