simple time series: plot works, qplot gives error

21 views
Skip to first unread message

Christian Lindig

unread,
Feb 11, 2009, 1:46:04 PM2/11/09
to ggplot2
I have a problem with a seemingly simple time series but I am also an
R and ggplot2 newbie. I'd like to plot a delay (an integer) over dates
using qplot. This results is an (internal) error from qplot:

> j <- dates[1:6]
> k <- drops$delay[1:6]
> j
[1] "2008-11-14" "2008-10-24" "2008-09-27" "2008-09-26" "2008-09-19"
[6] "2008-09-10"
> k
[1] 83 60 61 87 89 90
> class(j)
[1] "POSIXt" "POSIXlt"

> plot(j,k) # this works
> qplot(j,k) # this does not work, resulting in:

Error in if (length(range) == 1 || diff(range) == 0) { :
missing value where TRUE/FALSE needed

Obviously does the condition above not evaluate to a boolean but I
can't see where the problem is. This is with ggplot2 version 0.8.1

-- Christian

hadley wickham

unread,
Feb 11, 2009, 2:15:27 PM2/11/09
to Christian Lindig, ggplot2
Hi Christian,

There's a small bug in the labelling algorithm that crops up with
certain date ranges. You can work around it by explicitly specifying
the major, minor and format arguments to scale_x_datetime:

... + scale_x_datetime(major = "2 days", minor = "1 day", format = "%d/%m")

Regards,

Hadley
--
http://had.co.nz/

Felipe Carrillo

unread,
Feb 11, 2009, 2:24:48 PM2/11/09
to ggplot2, Christian Lindig
Is this what you want?

j <- c("2008-11-14","2008-10-24","2008-09-27","2008-09-26","2008-09-19","2008-09-10")
j
k <- c(83,60,61,87,89,90)
k
myplot <- data.frame(j,k)
myplot
qplot(j,k,data=myplot)
qplot(j,k,data=myplot,geom="bar")


Felipe D. Carrillo
Supervisory Fishery Biologist
Department of the Interior
US Fish & Wildlife Service
California, USA


--- On Wed, 2/11/09, Christian Lindig <lin...@gmail.com> wrote:

Christian Lindig

unread,
Feb 11, 2009, 3:37:08 PM2/11/09
to mazatla...@yahoo.com, ggplot2

On Feb 11, 2009, at 8:24 PM, Felipe Carrillo wrote:

> Is this what you want?
>
> j <-
> c
> ("2008
> -11
> -14","2008-10-24","2008-09-27","2008-09-26","2008-09-19","2008-09-10")

> k <- c(83,60,61,87,89,90)
> myplot <- data.frame(j,k)
> qplot(j,k,data=myplot)

This is what I want. Your code above works for me, but I don't know
why it doesn't work with my data (which I pull from an SQL query but
not assemble into a data frame for qplot).

h.wi...@gmail.com said:
> There's a small bug in the labelling algorithm that crops up with
> certain date ranges. You can work around it by explicitly specifying
> the major, minor and format arguments to scale_x_datetime:
>
> ... + scale_x_datetime(major = "2 days", minor = "1 day", format =
> "%d/%m")

This is interesting. It did not solve my problem, though.

Thanks for both of your help! I'll have to dig deeper.

-- Christian

Christian Lindig

unread,
Feb 11, 2009, 3:44:59 PM2/11/09
to mazatla...@yahoo.com, ggplot2

On Feb 11, 2009, at 8:24 PM, Felipe Carrillo wrote:

> Is this what you want?
>
> j <-

> c
> ("2008
> -11

> -14","2008-10-24","2008-09-27","2008-09-26","2008-09-19","2008-09-10")
> j
> k <- c(83,60,61,87,89,90)
> k
> myplot <- data.frame(j,k)
> myplot
> qplot(j,k,data=myplot)
> qplot(j,k,data=myplot,geom="bar")

Actually, this is not what I want because your j does not contain
dates but strings. If you convert j into dates, the problem will show
up again:

> j <-
c
("2008
-11

-14","2008-10-24","2008-09-27","2008-09-26","2008-09-19","2008-09-10")

> k <- c(83,60,61,87,89,90)
>
> j <- as.POSIXlt(j)
> qplot(j,k)


Error in if (length(range) == 1 || diff(range) == 0) { :
missing value where TRUE/FALSE needed

-- Christian

hadley wickham

unread,
Feb 11, 2009, 4:52:05 PM2/11/09
to Christian Lindig, mazatla...@yahoo.com, ggplot2
On Wed, Feb 11, 2009 at 2:37 PM, Christian Lindig <lin...@gmail.com> wrote:
>
> On Feb 11, 2009, at 8:24 PM, Felipe Carrillo wrote:
>
>> Is this what you want?
>>
>> j <-
>> c("2008-11-14","2008-10-24","2008-09-27","2008-09-26","2008-09-19","2008-09-10")

>> k <- c(83,60,61,87,89,90)
>> myplot <- data.frame(j,k)
>> qplot(j,k,data=myplot)
>
> This is what I want. Your code above works for me, but I don't know why it
> doesn't work with my data (which I pull from an SQL query but not assemble
> into a data frame for qplot).
>
> h.wi...@gmail.com said:
>>
>> There's a small bug in the labelling algorithm that crops up with
>> certain date ranges. You can work around it by explicitly specifying
>> the major, minor and format arguments to scale_x_datetime:
>>
>> ... + scale_x_datetime(major = "2 days", minor = "1 day", format =
>> "%d/%m")
>
> This is interesting. It did not solve my problem, though.
>
> Thanks for both of your help! I'll have to dig deeper.

If you could send a small sample of your dataset (e.g. with dput) I
could look into it in more detail.
Regards,

Hadley

--
http://had.co.nz/

Felipe Carrillo

unread,
Feb 11, 2009, 4:54:32 PM2/11/09
to Christian Lindig, ggplot2
What about this:
# Generate some dates
j <- seq(Sys.Date(),len=6,by="1 day")
j
k <- c(83,60,61,87,89,90)
myplot <- data.frame(j,k)
myplot
plot(j,k) # this works
qplot(j,k,data=myplot)

# Or this with your actual data

x <- "IDdate value
2009-02-11 83
2009-02-12 60
2009-02-13 61
2009-02-14 87
2009-02-15 89
2009-02-16 90"
myplot <- read.table(textConnection(x),header=TRUE)
myplot
#myplot$IDdate <- as.Date(myplot$IDdate,"%m/%d/%Y")
qplot(IDdate,value,data=myplot)

--- On Wed, 2/11/09, Christian Lindig <lin...@gmail.com> wrote:

> From: Christian Lindig <lin...@gmail.com>
> Subject: Re: simple time series: plot works, qplot gives error
> To: mazatla...@yahoo.com
> Cc: "ggplot2" <ggp...@googlegroups.com>
> Date: Wednesday, February 11, 2009, 12:44 PM
> On Feb 11, 2009, at 8:24 PM, Felipe Carrillo wrote:
>
> > Is this what you want?
> >
> > j <-

> c("2008-11-14","2008-10-24","2008-09-27","2008-09-26","2008-09-19","2008-09-10")


> > j
> > k <- c(83,60,61,87,89,90)
> > k
> > myplot <- data.frame(j,k)
> > myplot
> > qplot(j,k,data=myplot)
> > qplot(j,k,data=myplot,geom="bar")
>
> Actually, this is not what I want because your j does not
> contain dates but strings. If you convert j into dates, the
> problem will show up again:
>
> > j <-

> c("2008-11-14","2008-10-24","2008-09-27","2008-09-26","2008-09-19","2008-09-10")

Christian Lindig

unread,
Feb 12, 2009, 10:23:20 AM2/12/09
to ggplot2

The solution is NOT to use POSIXlt but POSIXct for date scanning, as
Hadley Wickham pointed out to me in a private email. Thanks to
everyone! The code below now works.

library(ggplot2)


j <-
c
("2008
-11

-14","2008-10-24","2008-09-27","2008-09-26","2008-09-19","2008-09-10")
k <- c(83,60,61,87,89,90)

j <- as.POSIXct(j)
p <- qplot(j,k)
print(p)

-- Christian


Reply all
Reply to author
Forward
0 new messages