Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scale_x_date or maybe date_format has problems with some dates (e.g. 4-01-31) #25

Closed
smu opened this issue Jul 10, 2012 · 5 comments
Closed

Comments

@smu
Copy link

smu commented Jul 10, 2012

With some dates, e.g. dates like 4-01-31 or lower is get the following error message:

Error in charToDate(x) :
character string is not in a standard unambiguous format

Here is an example:

library(ggplot2)
library(scales)


start <- as.Date("100-01-31")
end <- as.Date("200-01-31")
dates <- seq.Date(start, end, by='months')
df <- data.frame(date=dates, value=rnorm(1201))

p <- ggplot(df, aes(date, value)) + geom_point() +
    scale_x_date('\nYEAR AD', 
         breaks = date_breaks("10 years"), 
         labels = date_format("%Y"))
print(p)

start <- as.Date("4-01-31")
end <- as.Date("101-01-31")
dates <- seq.Date(start, end, by='months')
df <- data.frame(date=dates, value=rnorm(length(dates)))
p %+% df  # this fails


start <- as.Date("5-01-31")
end <- as.Date("101-01-31")
dates <- seq.Date(start, end, by='months')
df <- data.frame(date=dates, value=rnorm(length(dates)))
p %+% df  # this works

> sessionInfo()
R version 2.15.1 (2012-06-22)
Platform: i486-pc-linux-gnu (32-bit)

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8
 [7] LC_PAPER=C                 LC_NAME=C
 [9] LC_ADDRESS=C               LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] scales_0.2.1  ggplot2_0.9.1

loaded via a namespace (and not attached):
 [1] colorspace_1.1-1   dichromat_1.2-4    digest_0.5.2       grid_2.15.1
 [5] labeling_0.1       MASS_7.3-18        memoise_0.1        munsell_0.3
 [9] plyr_1.7.1         proto_0.3-9.2      RColorBrewer_1.0-5 reshape2_1.2.1
[13] stringr_0.6
@wch
Copy link
Member

wch commented Jul 10, 2012

It looks like the problem is happening because sometimes the breaks have negative years, and as.Date() doesn't handle these well.

x <- as.Date(c("0001-01-24", "0500-02-02"))
as.character(x)
# "0001-01-24" "0500-02-02"
as.Date(as.character(x))

# Minus 391 days gives negative year
as.character(x - 391)
# "-001-12-30" "0499-01-07"
as.Date(as.character(x - 391))
# Error during wrapup: character string is not in a standard unambiguous format

@smu
Copy link
Author

smu commented Jul 11, 2012

A right, I forgot that I asked about this on r-help [1], but never got an answer.
In this case it is a bug in R?

[1] http://r.789695.n4.nabble.com/Date-object-and-origin-BC-tp4173163.html

@wch
Copy link
Member

wch commented Jul 11, 2012

I just asked the same thing on r-devel, but no helpful replies yet.

https://stat.ethz.ch/pipermail/r-devel/2012-July/064398.html

It might be a "feature" instead of a bug, at least to the core R developers.

@hadley
Copy link
Member

hadley commented Jun 10, 2015

Fixing this is going to be hard - I think it you could extract the necessary bits out of cut.Date() but it seems like a lot of work for little gain.

@hadley hadley closed this as completed Jun 10, 2015
@MicheleCazzola
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants