tags:

views:

152

answers:

2

I'd like to plot this vs. time, with the actual dates (years actually, 1997,1998...2010). The dates are in a raw format, ala SAS, days since 1960 (hence as.date conversion). If I convert the dates using as.date to variable x, and do the GAM plot, I get an error. It works fine with the raw day numbers. But I want the plot to display the years (data are not equally spaced).

structure(list(site = c(928L, 928L, 928L, 928L, 928L, 928L, 928L, 
928L, 928L, 928L, 928L, 928L, 928L, 928L, 928L, 928L, 928L, 928L, 
928L, 928L, 928L, 928L, 928L, 928L, 928L, 928L), date = c(13493L, 
13534L, 13566L, 13611L, 13723L, 13752L, 13804L, 13837L, 13927L, 
14028L, 14082L, 14122L, 14150L, 14182L, 14199L, 16198L, 16279L, 
16607L, 16945L, 17545L, 17650L, 17743L, 17868L, 17941L, 18017L, 
18092L), y = c(7L, 7L, 17L, 18L, 17L, 17L, 10L, 3L, 17L, 24L, 
11L, 5L, 5L, 3L, 5L, 14L, 2L, 9L, 9L, 4L, 7L, 6L, 1L, 0L, 5L, 
0L)), .Names = c("site", "date", "y"), class = "data.frame", row.names = c(NA, 
-26L))

 sgam1 <- gam(sites$y ~ s(sites$date))
    sgam <- predict(sgam1, se=TRUE)
    plot(sites$date,sites$y,xaxt="n", xlab='Time', ylab='Counts')
    x<-as.Date(sites$date, origin="1960-01-01")
    axis(1, at=1:26,labels=x)

 lines(sites$date,sgam$fit, lty = 1)
 lines(sites$date,sgam$fit + 1.96* sgam$se, lty = 2)
 lines(sites$date,sgam$fit - 1.96* sgam$se, lty = 2)

ggplot2 has a solution (it doesn't mind the as.date thing) but it gives me other problems...

A: 

Use the origin= argument to as.Date() to specify a particular offset:

R> as.Date(c(928, 928, 930), origin="1960-01-01")
[1] "1962-07-17" "1962-07-17" "1962-07-19"
R> 

Once you have a Date type for your data, you have options for formatting the axis as you wish.

Dirk Eddelbuettel
Thanks for your input. I've converted the dates using as.Date, however the actual raw day values are what show up on the graph- such that the x axis goes from ~14000:18000, which are days since 1960. I'd like it to show the years.
Nate
Show the commands. If you get -14000:18000 then this got converted back to numerics, ie you didn't keep the date. Try eg 'format(as.Date("1970-01-01")-365*2, "%Y")' which will yield 1968 as expected.
Dirk Eddelbuettel
as.Date(sites$date, origin="1960-01-01") is what I used. I don't know why it got converted back...
Nate
A: 
sites <- read.table("349.txt", header = TRUE, sep = "\t", quote="\"", dec=".") 
p<-as.Date(sites$date, origin="1960-01-01")
sgam1 <- gam(sites$y ~ s(sites$date))
sgam <- predict(sgam1, se=TRUE)
plot(p,sites$y, xlab='Time', ylab='Counts')
 lines(p,sgam$fit, lty = 1)
 lines(p,sgam$fit + 1.96* sgam$se, lty = 2)
 lines(p,sgam$fit - 1.96* sgam$se, lty = 2)

This works!

Nate