R-绘制一条缺少NA值的线 [英] R - Plotting a line with missing NA values

查看:88
本文介绍了R-绘制一条缺少NA值的线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下data.frame,子集"

I have the following data.frame, "subset"

Time            A   B   C
2016-10-07 06:16:46 NA  NA  41 
2016-10-07 06:26:27 40  39  42
2016-10-07 06:38:23 NA  40  NA
2016-10-07 06:41:06 42  42  44
2016-10-07 06:41:06 NA  42  44
2016-10-07 06:41:06 NA  42  44
2016-10-07 06:41:07 44  43  48
2016-10-07 06:41:41 NA  43  48
2016-10-07 06:42:44 45  42  48
2016-10-07 06:48:40 46  45  48

我想绘制一个图,其中时间"是x轴,"A"是一条线,"B"和"C"是点.

I would like to have a plot where "Time" is the x-axis, "A" is a line and "B" and "C" are points.

但是,当我绘制此图形时,出现在"A"上的唯一一行是连接最后两个点(45和46)的那一行,因为这是"A"中仅有的两个连续值.该图忽略了"A"值之间的NA,而不是通过NA填充连接这些值的线.为此,我使用以下代码:

However, when i plot this, the only line that appears for "A" is the one connecting the last 2 dots (45 and 46), because these are the only 2 consecutive values in "A". The plot ignores the NAs between the values of "A" instead of potting a line connecting these values through the NAs. To do so, I use the following code:

plot(subset$Time,subset$A,type="l",ylim=c(min(subset$B,na.rm=TRUE)-5,max(subset$C,na.rm=TRUE)+5),xlim=c(as.POSIXct(min(subset$Time)),as.POSIXct(max(subset$Time))))
lines(subset$Time,subset$B,type="p",col=27)
lines(subset$Time,subset$C,type="p",col=134)

我尝试了诸如na.omit()或na.approx()之类的解决方案,但是,这些解决方案似乎只有在我将"A"单独绘制在独立图中时才起作用,它们似乎无法结合使用"Time","B"和"C"都在同一图中.

I have tried solutions such as na.omit() or na.approx(), however these seem to work only if I would plot "A" separately in a stand-alone plot, they do not seem to work in conjunction with "Time" and "B" and "C" all in the same plot.

推荐答案

您可以使用索引而不是na.omit():类似的事情应该可以做到:

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)

这篇关于R-绘制一条缺少NA值的线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆