scale_x_continuous(limits=) with "Date" class x variable

1,242 views
Skip to first unread message

Ivan Alves

unread,
Jan 9, 2009, 7:33:04 AM1/9/09
to ggplot2
Dear Hadley and group members,

I am trying to restrict the x axis to a subset of the range of a
"Date" class variable in the data.frame serving as "x". Without the
scale_x_continuous layer the plot is fine.

ggplot(data=cast(collateral,Date+asset~haircut, sum, na.rm=TRUE), aes
(x=Date)) + geom_bar(aes(y=after, fill=asset),stat="identity")

However, once I add the layer as either

+ scale_x_continuous(limits = c(as.Date("2007-07-31"), as.Date
("2009-01-01")))

or

+ scale_x_continuous(limits = c(as.numeric(as.Date("2007-07-31")),
as.numeric(as.Date("2009-01-01"))))

I get the same error:

Error in if (check.width && length(widths) > 1 && sd(widths) > 1e-06)
{ :
missing value where TRUE/FALSE needed

Is this a bug, or just that I am assuming too much about "limits"?

For your information I also tried the same domain restrictions with
the "economics" dataframe used in section 5.9 of Hadley's future book
and there it seems to only work with the as.numeric conversion, but
loosing the "format" of the dates.

unemp + scale_x_continuous(limits = c(as.Date("2000-07-31"), as.Date
("2007-01-01")))
unemp + scale_x_continuous(limits = c(as.numeric(as.Date
("2000-07-31")), as.numeric(as.Date("2007-01-01"))))

The error there when using only as.Date is the following:

Error in grid.pretty(range) : 'range' must be numeric
In addition: Warning message:
In expand_range(.$output_set(), .$.expand[1], .$.expand[2]) :
Incompatible methods ("+.Date", "Ops.difftime") for "+"

many thanks in advance for any guidance
Kind regards,

Ivan

Charlotte

unread,
Jan 10, 2009, 11:50:37 AM1/10/09
to ggplot2
Ivan,

I think using scale_x_date will solve your problem. At least:

unemp + scale_x_date(limits = c(as.Date("2000-07-31"), as.Date
("2007-01-01")))

works without errors for me.

Charlotte

Ivan Alves

unread,
Jan 11, 2009, 4:50:22 PM1/11/09
to ggplot2
Many thanks to Matthew, Hadley and Charlotte for pointing me to the
obvious. Sorry, I was to concentrated in learning the possibilities
of ggplot2 with the book and forgot the web site!

This notwithstanding, I get the same error using scale_x_date() as
scale_x_continuous, namely:

Error in if (check.width && length(widths) > 1 && sd(widths) > 1e-06)
{ :
missing value where TRUE/FALSE needed

Again, it works fine without using limits, so I am not sure this is
not a bug.

Kind regards,

Ivan

hadley wickham

unread,
Jan 12, 2009, 9:18:35 AM1/12/09
to Ivan Alves, ggplot2
> This notwithstanding, I get the same error using scale_x_date() as
> scale_x_continuous, namely:
>
> Error in if (check.width && length(widths) > 1 && sd(widths) > 1e-06)
> { :
> missing value where TRUE/FALSE needed
>
> Again, it works fine without using limits, so I am not sure this is
> not a bug.

In that case, can you please send me a small reproducible example so I
can look into it?

Thanks,

Hadley

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

Reply all
Reply to author
Forward
0 new messages