[R] 'matplot' for matrix with NAs: broken lines

Tao Shi shitao at hotmail.com
Thu May 6 23:06:37 CEST 2010


Thanks for the suggestion!

________________________________
> Date: Thu, 6 May 2010 13:40:04 -0700
> Subject: Re: [R] 'matplot' for matrix with NAs: broken lines
> From: djmuser at gmail.com
> To: shitao at hotmail.com
> CC: maechler at stat.math.ethz.ch; r-help at r-project.org
>
> Hi:
>
> If you intend to use your preferred solution, then I would suggest that you increase the size of
> the plotted points relative to the thickness of the adjoining lines; in your last line of code, something
> like
>
>
> xyplot(y~x, group=g, data=tmp2, type="b", cex = 2, pch = 16)
>
> This way, it will be easier to spot where data values are missing.
>
> HTH,
> Dennis
>
> On Thu, May 6, 2010 at 10:44 AM, Tao Shi> wrote:
>
>
>
> I just found out that my "does this by default" statement (by which I was referring to the ability to automatically connect two points with a NA in the middle in a time series) is wrong! Actually, all plotting functions, i.e. plot, matplot and xyplot, don't plot NAs. The solution I came up with is convert the data to "long" table, remove NAs, and then use xyplot. See example below:
>
>
>
>
> set.seed(1234)
>
> a=b=matrix(rnorm(9), 3,3)
>
> b[2,2]=NA
>
> matplot(a, type="b")
>
[[elided Hotmail spam]]
>
> matplot(b, type="l") ## Now my data for the second column are missing from the graph
>
>
>
>
>
> ## my solution
>
> tmp1 <- data.frame(g=rep(1:3,each=3), x=rep(1:3,3), y=c(b))
>
> xyplot(y~x, group=g, data=tmp1, type="b", pch=c("1","2","3")) ## there is still no line connecting two "2"s.
>
>
>
> tmp2 <- tmp1[!is.na(tmp1$y),]
>
> xyplot(y~x, group=g, data=tmp2, type="b")
>
> ## this is what I want, b/c it's easier for me to keep track of both trend and missing values. The original post was really asking whether a simple change of some parameters in matplot can do this. Now, I guess not.
>
>
>
>
> ...Tao
>
>
>
>
>
> ----------------------------------------
>
>> From: maechler at stat.math.ethz.ch
>
>> Date: Thu, 6 May 2010 18:34:22 +0200
>
>> To: shitao at hotmail.com
>
>> CC: ggrothendieck at gmail.com; r-help at r-project.org
>
>> Subject: Re: [R] 'matplot' for matrix with NAs: broken lines
>
>>
>
>>>>>>> "TS" == Tao Shi
>
>>>>>>> on Wed, 5 May 2010 20:11:26 +0000 writes:
>
>>
>
>> TS> Thanks, Gabor! So, there is no way I can change some graphic parameters in 'matplot' to get this?
>
>>
>
>>
>
>> TS> I forgot to mention that I purposely use type="b", so I know where the missing data are. With imputed data, either using "b" or "l", there is no way to keep track of NAs. Plus, in my real data sometimes there is only one non-missing value in a particular column and na.approx can't work (well I could selectively impute the NAs ... )
>
>
>>
>
>> TS> So far, my best solution to this is to use "xyplot". It does this by default, but of course I need some data manipulation first.
>
>>
>
>> "does this by default" meaning what?
>
>> I don't think it does impute missing, does it?
>
>>
>
>> Can you elaborate, using your example (below)?
>
>>
>
>> I found Gabor's answer appropriate,
>
>> I really cannot see why matplot() should behave differently here...
>
>>
>
>> ----
>
>>
>
>> Martin Maechler
>
>>
>
>>
>
>>
>
>>
>
>> TS> ----------------------------------------
>
>>>> From: ggrothendieck at gmail.com
>
>>>> Date: Wed, 5 May 2010 15:45:44 -0400
>
>>>> Subject: Re: [R] 'matplot' for matrix with NAs: broken lines
>
>>>> To: shitao at hotmail.com
>
>>>> CC: r-help at r-project.org
>
>>>>
>
>>>> Try this:
>
>>>>
>
>>>> library(zoo)
>
>>>> matplot(na.approx(b), type = "l")
>
>>>>
>
>>>> On Wed, May 5, 2010 at 2:30 PM, Tao Shi wrote:
>
>>>>>
>
>>>>> Hi list,
>
>>>>>
>
>>>>> I know that points involving NAs are not plotted in 'matplot', but when I plot them as lines, I still want the lines to connect all the points (i.e. not broken where there are NAs). Please see the example below. How can I achieve this in 'matplot'? If I can't, any good alternatives so I don't have to use 'plot' + 'lines' and loop through all the columns.
>
>
>>>>>
>
>>>>> Many thanks!
>
>>>>>
>
>>>>> ...Tao
>
>>>>>
>
>>>>>> set.seed(1234)
>
>>>>>> a=b=matrix(rnorm(9), 3,3)
>
>>>>>> b[2,2]=NA
>
>>>>>> matplot(a, type="b")
>
>> TS> [[elided Hotmail spam]]
>
>>>>>> matplot(b, type="l") ## Now my data for the second column are missing from the graph
>
>>>>>
>
>>>>>
>
>>>>> _________________________________________________________________
>
>>>>> Hotmail is redefining busy with tools for the New Busy. Get more from your inbox.
>
>>>>>
>
>>>>> N:WL:en-US:WM_HMP:042010_2
>
>>>>> ______________________________________________
>
>>>>> R-help at r-project.org mailing list
>
>>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>
>>>>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>
>>>>> and provide commented, minimal, self-contained, reproducible code.
>
>>>>>
>
>>
>
>> TS> _________________________________________________________________
>
>> TS> Hotmail has tools for the New Busy. Search, chat and e-mail from your inbox.
>
>>
>
>> TS> N:WL:en-US:WM_HMP:042010_1
>
>> TS> ______________________________________________
>
>> TS> R-help at r-project.org mailing list
>
>> TS> https://stat.ethz.ch/mailman/listinfo/r-help
>
>> TS> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>
>> TS> and provide commented, minimal, self-contained, reproducible code.
>
>
>
> _________________________________________________________________
>
> Hotmail has tools for the New Busy. Search, chat and e-mail from your inbox.
>
>
>
> N:WL:en-US:WM_HMP:042010_1
>
> ______________________________________________
>
> R-help at r-project.org mailing list
>
> https://stat.ethz.ch/mailman/listinfo/r-help
>
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>
> and provide commented, minimal, self-contained, reproducible code.
>
>
 		 	   		  
_________________________________________________________________
The New Busy think 9 to 5 is a cute idea. Combine multiple calendars with Hotmail. 

PID28326::T:WLMTAGL:ON:WL:en-US:WM_HMP:042010_5


More information about the R-help mailing list