[R] Beginner Q- dates in the xrange?

Gabor Grothendieck ggrothendieck at gmail.com
Thu Sep 17 03:19:41 CEST 2009


You want to make sure your dates are of Date class, not character class.
Since this is evidently an irregular time series you might want to try out
plot.zoo:

library(zoo)

# convert to zoo
z <- with(results, zoo(DIFF, as.Date(date)))

# plot
plot(cbind(z, z), screen = 1, type = c("p", "l"), lwd = 1.5, pch = 15,
	col = 1:2, xaxt = "n", xlab = "", ylab = "Differential")

# dd is sequence of dates.  Since ts can only represent regular
# time series converting to ts fills them in and then we convert back
dd <- as.Date(time(as.zoo(as.ts(z))))

# mm are dates at the beginning of each month
mm <- as.Date(unique(as.yearmon(dd)))

# draw axes with labeled months and unlabeled days
axis(1, mm, lab = format(mm, "%b"))
axis(1, dd, lab = FALSE, tcl = -0.3)

There are similar examples in ?plot.zoo.  Also see R News 4/1
for dates and see the three zoo vignettes.  ?plot.zoo and the vignettes
have many examples including ones quite similar to the above.

On Wed, Sep 16, 2009 at 8:40 PM, Wells Oliver <wells at submute.net> wrote:
> Follow up with actual data and what I'm trying to do. Current R code/script:
> plot(results$DIFF, xaxt="n", ylab="Differential", xlab="2009")
> daterange=c(as.POSIXlt(min(results$date)),as.POSIXlt(max(results$date)))
> axis.POSIXct(1, at=seq(daterange[1], daterange[2], by="month"), format="%b")
> lines(results$DIFF, type="l", lwd=1.5, col="red", pch=15)
>
> dput(results):
>
> structure(list(date = c("2010-04-12", "2010-04-14", "2010-04-15",
> "2010-04-16", "2010-04-17", "2010-04-18", "2010-04-19", "2010-04-20",
> "2010-04-21", "2010-04-23", "2010-04-24", "2010-04-25", "2010-04-27",
> "2010-04-28", "2010-04-29", "2010-04-30", "2010-05-01", "2010-05-02",
> "2010-05-03", "2010-05-04", "2010-05-05", "2010-05-06", "2010-05-07",
> "2010-05-08", "2010-05-09", "2010-05-10", "2010-05-11", "2010-05-12",
> "2010-05-13", "2010-05-14", "2010-05-15", "2010-05-16", "2010-05-18",
> "2010-05-19", "2010-05-20", "2010-05-21", "2010-05-22", "2010-05-23",
> "2010-05-25", "2010-05-26", "2010-05-27", "2010-05-28", "2010-05-29",
> "2010-05-30", "2010-05-31", "2010-06-01", "2010-06-02", "2010-06-04",
> "2010-06-05", "2010-06-06", "2010-06-08", "2010-06-09", "2010-06-10",
> "2010-06-11", "2010-06-12", "2010-06-13", "2010-06-14", "2010-06-15",
> "2010-06-16", "2010-06-17", "2010-06-18", "2010-06-19"), DIFF = c(0,
> 7, 3, 8, 2, -3, 0, -5, -6, -12, -10, -5, -11, -7, -5, -3, 5,
> 6, 7, 3, 6, 9, 7, 5, 12, 10, 12, 13, 1, 5, 6, 4, 3, -1, 0, -6,
> -5, 2, -2, 2, -3, 0, 4, 2, 4, 9, 12, 16, 13, 14, 16, 18, 15,
> 14, 21, 23, 24, 26, 27, 26, 27, 22)), .Names = c("date", "DIFF"
> ), row.names = c("1", "2", "3", "4", "5", "6", "7", "8", "9",
> "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20",
> "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31",
> "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42",
> "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53",
> "54", "55", "56", "57", "58", "59", "60", "61", "62"), class = "data.frame")
>
> However, on the plot, there are no values for the dates; it seems as though
> the axis.POSIXct call is failing. Thanks for any tips.
>
> On Wed, Sep 16, 2009 at 7:15 PM, Wells Oliver <wells at submute.net> wrote:
>
>> Hi all- terribly beginner question here, but I can't figure out how to put
>> dates as the values for my x range in a plot. Example:
>> xrange <- results$current
>> yrange <- results$DIFF
>>
>> plot(xrange, yrange, type="n", xlab="Date", ylab="Differential")
>> lines(results$DIFF, type="l", lwd=1.5, col="red")
>>
>> Where xrange is:
>>
>> > xrange
>>  [1] "2010-04-12" "2010-04-14" "2010-04-15" "2010-04-16" "2010-04-17"
>> "2010-04-18" "2010-04-19" "2010-04-20" "2010-04-21" "2010-04-23"
>> "2010-04-24" "2010-04-25"
>> [13] "2010-04-27" "2010-04-28" "2010-04-29" "2010-04-30" "2010-05-01"
>> "2010-05-02" "2010-05-03" "2010-05-04" "2010-05-05" "2010-05-06"
>> "2010-05-07" "2010-05-08"
>> [25] "2010-05-09" "2010-05-10" "2010-05-11" "2010-05-12" "2010-05-13"
>> "2010-05-14" "2010-05-15" "2010-05-16" "2010-05-18" "2010-05-19"
>> "2010-05-20" "2010-05-21"
>> [37] "2010-05-22" "2010-05-23" "2010-05-25" "2010-05-26" "2010-05-27"
>> "2010-05-28" "2010-05-29" "2010-05-30" "2010-05-31" "2010-06-01"
>> "2010-06-02" "2010-06-04"
>> [49] "2010-06-05" "2010-06-06" "2010-06-08" "2010-06-09" "2010-06-10"
>> "2010-06-11" "2010-06-12" "2010-06-13" "2010-06-14" "2010-06-15"
>> "2010-06-16" "2010-06-17"
>> [61] "2010-06-18" "2010-06-19"
>>
>> The error being:
>>
>> > plot(xrange, yrange, type="n", xlab="Date", ylab="Differential")
>> Error in plot.window(...) : need finite 'xlim' values
>> In addition: Warning messages:
>> 1: In xy.coords(x, y, xlabel, ylabel, log) : 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
>>
>> Any tips on how to generate a nicely-formatted plot w/ dates in the x axis?
>> Thanks.
>>
>> --
>> Wells Oliver
>> wells at submute.net
>>
>
>
>
> --
> Wells Oliver
> wells at submute.net
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>




More information about the R-help mailing list