[R] plotting NAs

Gabor Grothendieck ggrothendieck at gmail.com
Sat Mar 8 19:09:31 CET 2008


Please do not cross post.

Note from yesterday:
https://stat.ethz.ch/pipermail/r-help/2008-March/156586.html



On Sat, Mar 8, 2008 at 12:40 PM, John McHenry <john_d_mchenry at yahoo.com> wrote:
> Hi WizaRds,
>
> (I've cross-posted to r-sig-finance because a lot of people there use 'zoo' objects).
>
> I'm trying to plot 2 time series lined up with par(mfrow=c(2,1)), so that the time index is the same for the two series. The data have monthly frequency.
>
> An example will illustrate:
>
> # data for these dates (monthly frequency) do not have NAs:
> require(zoo)
> monthly.dates<- as.Date(c(
>    "1991-01-31",
>    "1991-02-28",
>    "1991-03-31",
>    "1991-04-30",
>    "1991-05-30"
> ))
> monthly.only.data<- 1:length(monthly.dates)
> # data for these extra dates (daily frequency) do have NAs interspersed with
> # non-NA data having monthly frequency:
> daily.dates<- seq(as.Date("1991-06-01"), as.Date("1991-07-31"), by=1)
> tmp<- zoo(NA, order.by=c(monthly.dates, daily.dates))
> z<- cbind(tmp, tmp)
> colnames(z)<- NULL
> monthly.in.daily.data<- (1:2)/10
> z[index(z) %in% monthly.dates, 1]<- monthly.only.data
> z[index(z) %in% as.Date(c("1991-06-30","1991-07-31")), 2]<-
>        monthly.in.daily.data
>
> # this creates a block of data like that listed below.
>
> Here's my problem: I want to plot lines through all non-NA data.
> This is straight forward with column 1, but not with column 2.
> Why? Because the monthly-frequency data in column 2 have NAs interspersed.
>
> plot(z, type="l")
>
> does not plot a line through the points in column 2.
> (It's not a problem with 'plot.zoo'; same thing happens if you
> use 'plot' on regular data like:
> plot(c(1, NA, 2, 3, NA, 4), type="l")
>
> The following "works" in so far as it will plot the correct
> picture,
>
> tmp<- zoo(NA, seq(min(index(z)), max(index(z)), by=1))
> z<- cbind(tmp, z); z<- z[,-1]
> plot(na.omit(z), type="l")
> plot(z[!apply(is.na(z), 1, sum)==2,], type="l")
>
> but for various reasons too long to go into here
> I want to know how to plot lines through the non-NA data in
> objects like:
> c(1, NA, 2, 3, NA, 4)
> without having to strip out the NAs with the !apply(is.na(z), 1, sum)==2
> hack.
>
> What do I do???
>
> Thanks guys!
>
> Jack.
>
> # p.s. here's that 'zoo' data:
> > z
> 1991-01-31  1  NA
> 1991-02-28  2  NA
> 1991-03-31  3  NA
> 1991-04-30  4  NA
> 1991-05-30  5  NA
> 1991-06-01 NA  NA
> 1991-06-02 NA  NA
> 1991-06-03 NA  NA
> 1991-06-04 NA  NA
> 1991-06-05 NA  NA
> 1991-06-06 NA  NA
> 1991-06-07 NA  NA
> 1991-06-08 NA  NA
> 1991-06-09 NA  NA
> 1991-06-10 NA  NA
> 1991-06-11 NA  NA
> 1991-06-12 NA  NA
> 1991-06-13 NA  NA
> 1991-06-14 NA  NA
> 1991-06-15 NA  NA
> 1991-06-16 NA  NA
> 1991-06-17 NA  NA
> 1991-06-18 NA  NA
> 1991-06-19 NA  NA
> 1991-06-20 NA  NA
> 1991-06-21 NA  NA
> 1991-06-22 NA  NA
> 1991-06-23 NA  NA
> 1991-06-24 NA  NA
> 1991-06-25 NA  NA
> 1991-06-26 NA  NA
> 1991-06-27 NA  NA
> 1991-06-28 NA  NA
> 1991-06-29 NA  NA
> 1991-06-30 NA 0.1
> 1991-07-01 NA  NA
> 1991-07-02 NA  NA
> 1991-07-03 NA  NA
> 1991-07-04 NA  NA
> 1991-07-05 NA  NA
> 1991-07-06 NA  NA
> 1991-07-07 NA  NA
> 1991-07-08 NA  NA
> 1991-07-09 NA  NA
> 1991-07-10 NA  NA
> 1991-07-11 NA  NA
> 1991-07-12 NA  NA
> 1991-07-13 NA  NA
> 1991-07-14 NA  NA
> 1991-07-15 NA  NA
> 1991-07-16 NA  NA
> 1991-07-17 NA  NA
> 1991-07-18 NA  NA
> 1991-07-19 NA  NA
> 1991-07-20 NA  NA
> 1991-07-21 NA  NA
> 1991-07-22 NA  NA
> 1991-07-23 NA  NA
> 1991-07-24 NA  NA
> 1991-07-25 NA  NA
> 1991-07-26 NA  NA
> 1991-07-27 NA  NA
> 1991-07-28 NA  NA
> 1991-07-29 NA  NA
> 1991-07-30 NA  NA
> 1991-07-31 NA 0.2
>
>
>
>
>
> ---------------------------------
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list