如何避免R中matplot中缺少值导致的间隙?

时间:2017-08-09 17:53:06

标签: r na missing-data

我有一个使用matplot绘制一些数据的函数。数据结构如下:

test = data.frame(x = 1:10, a = 1:10, b = 11:20)
matplot(test[,-1])
matlines(test[,1], test[,-1])

到目前为止一切顺利。但是,如果数据集中存在缺失值,则结果图中存在间隙,我希望通过连接间隙的边缘来避免这些间隙。

test$a[3:4] = NA
test$b[7] = NA
matplot(test[,-1])
matlines(test[,1], test[,-1]) 

enter image description here

在实际情况下,这是在一个函数内部,矩阵的维数更大,行数,列数和非重叠缺失值的位置可能会在不同的调用之间发生变化,所以我喜欢找到一个可以灵活处理这个问题的解决方案。我还需要使用matlines

我在考虑填补内插数据的空白,但也许有更好的解决方案。

1 个答案:

答案 0 :(得分:1)

您可以使用na.interpolation包中的imputeTS功能:

test = data.frame(x = 1:10, a = 1:10, b = 11:20)
test$a[3:4] = NA
test$b[7] = NA
matplot(test[,-1])
matlines(test[,1], test[,-1])

library('imputeTS')

test <- na.interpolation(test, option = "linear")
matplot(test[,-1])
matlines(test[,1], test[,-1])

enter image description here