开发者

R plot- SGAM plot counts vs. time - how do I get dates on the x-axis?

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,开发者_JAVA技巧 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...


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.


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜