Hi Aidan, str is your friend:
str(g)
'data.frame': 9 obs. of 3 variables: $ Date : chr "2011-12-23" "2011-12-30" "2012-01-06" "2011-12-23" ... $ variable: Factor w/ 3 levels "Price","Yield",..: 1 1 1 2 2 2 3 3 3 $ value : num 86.78 86.04 86.44 9.74 9.54 ... You haven't turned the Date variable into an actual date yet. Try: g$Date <- as.Date(g$Date) Hadley On Fri, Jan 6, 2012 at 9:12 AM, Aidan Corcoran
<aidan.corcoran11 at gmail.com> wrote:
Dear all,
ggplot gives me an error when trying to plot time series data using a
date variable as the x axis.
g<-structure(list(Date = c("2011-12-23", "2011-12-30", "2012-01-06",
"2011-12-23", "2011-12-30", "2012-01-06", "2011-12-23", "2011-12-30",
"2012-01-06"), variable = structure(c(1L, 1L, 1L, 2L, 2L, 2L,
3L, 3L, 3L), .Label = c("Price", "Yield", "CDS Spread"), class = "factor"),
? ?value = c(86.777, 86.037, 86.437, 9.737, 9.542, 9.683, 580.132,
? ?576.866, 573.564)), .Names = c("Date", "variable", "value"
), row.names = c(100L, 101L, 102L, 202L, 203L, 204L, 304L, 305L,
306L), class = "data.frame")
g
? ? ? ? ?Date ? variable value 100 2011-12-23 ? ? ?Price ?86.8 101 2011-12-30 ? ? ?Price ?86.0 102 2012-01-06 ? ? ?Price ?86.4 202 2011-12-23 ? ? ?Yield ? 9.7 203 2011-12-30 ? ? ?Yield ? 9.5 204 2012-01-06 ? ? ?Yield ? 9.7 304 2011-12-23 CDS Spread 580.1 305 2011-12-30 CDS Spread 576.9 306 2012-01-06 CDS Spread 573.6
gp<-ggplot(g,aes(x=Date,y=value,group=variable,lty=variable)) +geom_line() gp #THIS WORKS FINE (BUT AXIS LABELLING NOT VISIBLE WITH MORE DATA, HENCE I WOULD LIKE TO USE SCALE_X_DATE) gp<-gp+ ? scale_x_date() gp
Error in seq.int(r1$year, to$year, by) : 'from' must be finite In addition: Warning messages: 1: In get(x, envir = this, inherits = inh)(this, ...) : ?NAs introduced by coercion 2: In min(x) : no non-missing arguments to min; returning Inf 3: In max(x) : no non-missing arguments to max; returning -Inf 4: In min(x) : no non-missing arguments to min; returning Inf 5: In max(x) : no non-missing arguments to max; returning -Inf
In previous help requests, the workaround of specifying the unit was suggested: gp<-gp+ ? scale_x_date(major="years") but this doesn't work for me (same error). Any help is very much appreciated! Thanks in advance. Aidan R version 2.14.1 (2011-12-22) Platform: i386-pc-mingw32/i386 (32-bit) locale: [1] LC_COLLATE=English_Ireland.1252 ?LC_CTYPE=English_Ireland.1252 LC_MONETARY=English_Ireland.1252 [4] LC_NUMERIC=C ? ? ? ? ? ? ? ? ? ? LC_TIME=English_Ireland.1252 attached base packages: ?[1] datasets ?tools ? ? grDevices grid ? ? ?splines ? graphics ?stats ? ?tcltk ? ? utils [10] methods ? base other attached packages: ?[1] micEcon_0.6-6 ? ? ?miscTools_0.6-12 ? np_0.40-11 cubature_1.0 ? ? ? boot_1.3-3 ?[6] RODBC_1.3-3 ? ? ? ?sqldf_0.4-6.1 ? ? ?chron_2.3-41 gsubfn_0.5-7 ? ? ? DBI_0.2-5 [11] Haver_1.0 ? ? ? ? ?xtable_1.5-6 ? ? ? plm_1.2-7 sandwich_2.2-7 ? ? MASS_7.3-16 [16] Formula_1.0-1 ? ? ?nlme_3.1-102 ? ? ? bdsmatrix_1.0 RBloomberg_0.4-150 rJava_0.9-1 [21] gtools_2.6.2 ? ? ? gdata_2.8.2 ? ? ? ?ggplot2_0.8.9 proto_0.3-9.2 ? ? ?zoo_1.7-4 [26] reshape_0.8.4 ? ? ?plyr_1.6 ? ? ? ? ? svSocket_0.9-52 TinnR_1.0.3 ? ? ? ?R2HTML_2.2 [31] Hmisc_3.8-3 ? ? ? ?survival_2.36-10 loaded via a namespace (and not attached): [1] cluster_1.14.1 ? ? ? ?digest_0.5.0 ? ? ? ? ?lattice_0.20-0 RSQLite_0.10.0 [5] RSQLite.extfuns_0.0.1 svMisc_0.9-63
______________________________________________ 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.
Assistant Professor / Dobelman Family Junior Chair Department of Statistics / Rice University http://had.co.nz/