如何创建一个连续几年的情节作为R轴中的x轴?

时间:2013-01-10 21:20:55

标签: r days

我试图绘制连续两年(例如2010年11月5日至2011年3月30日)的温度数据,其中每年的天数为x轴值。例如:

temp<-c(30.1:40.1) # y axis
doy<-c(360:365,1:5) # x axis

请帮帮我。感谢。

1 个答案:

答案 0 :(得分:0)

temp<-c(30.1:40.1) # y axis
doy<-c(360:365,1:5) # x axis

doy2 <- c(360:365,c(1:5)+365)

plot(temp ~ doy2, xaxt="n", xlab = "doy")
axis(1,doy,at=doy2)

可替换地:

最严格的方法是使用R中的日期时间对象。然后R将'temp'数据识别为日期,因此在绘制时会给出正确的结果。日期时间对象很复杂,但如果你经常处理它们,它们值得学习:

temp<-c(30.1:40.1) # y axis
doy<-c(360:365,1:5) # x axis

doy2 <- c(360:365,c(1:5)+365)  #we sill need this to place numbers 1:5 into a new year (i.e. 365 days later)


doy.date <- as.Date("2011-01-01")   #Set a base date (choose the year that you will start with

doy.date <- doy.date + doy2 - 1  #add the days of year to the base date and subtract one (the base date was January 1st)

plot(temp ~ doy.date, xlab = "doy")    #plot as usual

#see documentation on dates
?date

#or for date with times:
?POSIXct