[R] What is wrong with this plotting?

Gabor Grothendieck ggrothendieck at gmail.com
Fri Jan 6 16:19:04 CET 2012


On Fri, Jan 6, 2012 at 5:49 AM,  <raphael.felber at art.admin.ch> wrote:
> Hello
>
> I have a data frame, called input, like this:
>
>           DateTime CO2_A1cont
> 1 2011-04-08 11:47:01         NA
> 2 2011-04-08 12:42:01    8.90000
> 3 2011-04-08 13:07:01         NA
> 4 2011-04-08 13:32:01         NA
> 5 2011-04-08 13:57:01   7.556482
> 6 2011-04-08 14:22:01         NA
> ....
> 57 2011-04-09 16:52:01   4.961558
>
> And like to plot this series with plot() and connected lines, no interruption by the NAs.
> I found that this code works:
>
> y<-input[,2]
> times <- DateTime
>
> plot(y~as.POSIXct(times, format="%d.%m. %H:%M"), type="l", data=na.omit(data.frame(y,times)))
>
> whereas this plot command...
>
>  plot(input[,2]~as.POSIXct(DateTime, format="%d.%m. %H:%M"), type="l", data=na.omit(data.frame(input[,2],DateTime)))
>
> ... produces the error:
>
> Error in model.frame.default(formula = input[, 2] ~ as.POSIXct(DateTime,  :
>  variable lengths differ (found for 'as.POSIXct(DateTime, format = "%d.%m. %H:%M")')
>
> I already checked the length of y, times, input[,2], DateTime, as.POSIXct(DateTime, format="%d.%m. %H:%M") which give all 57! nrow(data.frame(input[,2],DateTime)) and nrow(data.frame(y,times)) give both  57! Too.

Try this reading DF into a zoo object.  (tz="" has the effect of
setting its index to POSIXct using the current time zone -- you might
need tz = "GMT" depending on what you want):

library(zoo)
z <- read.zoo(DF, tz = "")
plot(na.approx(z))


Assuming DF is:

DF <- structure(list(DateTime = structure(1:6, .Label = c("2011-04-08
11:47:01",
"2011-04-08 12:42:01", "2011-04-08 13:07:01", "2011-04-08 13:32:01",
"2011-04-08 13:57:01", "2011-04-08 14:22:01"), class = "factor"),
    CO2_A1cont = c(NA, 8.9, NA, NA, 7.556482, NA)), .Names = c("DateTime",
"CO2_A1cont"), class = "data.frame", row.names = c(NA, -6L))


-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list