R用NA值绘制数据集 [英] R plotting a dataset with NA Values

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

问题描述

我正在尝试绘制一个由数​​字和R中的一些NA条目组成的数据集.

I'm trying to plot a dataset consisting of numbers and some NA entries in R.

V1,V2,V3
 2, 4, 3
NA, 5, 4
NA,NA,NA
NA, 7, 3
 6, 6, 9

应该在绘图中返回相同的行,就像我输入的一样:

Should return the same lines in the plot, as if I had entered:

V1,V2,V3
 2, 4, 3
 3, 5, 4
 4, 6, 3.5
 5, 7, 3
 6, 6, 9

我需要R要做的基本上是将数据集绘制为点,然后通过直线将这些点连接起来,由于数据集的大小,这将比实际计算每个插值的效率高得多数据集.因此,它不应通过计算插值(通过循环或类似方法)来实现,而应仅在图形上实现.

What I need R to do is basically plotting the dataset as points, an then connect these points by straight lines, which - due to the size of the dataset - would be much more efficient then the actual calculation of each interpolated value within the dataset. So it should not happen by computing interpolations (via loop or something like that), but instead just on the graph.

由于数据集有多列,而我目前正在使用Rs matplot函数对其进行可视化,因此应该有一种方法可以添加多条NA调整后的线(例如,在matpot()或lines()中).因此plot(...)会出现问题,因为它会覆盖当前的图形设备.

Since the dataset has multiple columns and I am currently using Rs matplot function to visualize it, there should be a way to add multiple NA-adjusted lines (as in matpot() or lines() for example). Hence plot(...) would be problematic because it overwrites the current graphics device.

推荐答案

mydf <- data.frame(V1=c(2,NA,NA,NA,6),V2=c(4,5,NA,7,6),V3=c(3,4,NA,3,9))

plot(NA,xlim=c(0,nrow(mydf)+1),ylim=c(min(mydf,na.rm=TRUE)-1,max(mydf,na.rm=TRUE)+1))
mapply(function(x,color){
    dat <- na.omit(cbind(1:length(x),x))
    lines(dat[,1],dat[,2],col=color)
},mydf,c("red","blue","green"))

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

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