[R] Beginner Q- dates in the xrange?

jim holtman jholtman at gmail.com
Thu Sep 17 02:49:01 CEST 2009


try this with your data:  you had character data in the 'date' field;
need to convert to Date

# convert the character 'date' to a Date object
results$date <- as.Date(results$date)
plot(results$date, results$DIFF, type='l')


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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?




More information about the R-help mailing list