【问题标题】:how to create a plot with continuous days of year of subsequent years as x axis in R?如何在R中创建一个连续年份的连续天数作为x轴的图?
【发布时间】:2013-01-10 21:20:54
【问题描述】:

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

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

请帮帮我。谢谢。

【问题讨论】:

    标签: r days


    【解决方案1】:
    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<-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
    

    【讨论】:

    • @user1968046 这解决了您的问题吗?如果是,请标记为答案。否则请解释为什么不,然后我会尽力提供进一步的帮助。
    • 谢谢汤姆·埃文斯。你的建议解决了我的问题。我很想知道其他方法也可以解释这个问题。谢谢。有时间请回复。
    • 我在上面的答案中添加了第二个建议。它使用日期时间对象,功能非常强大,但有点复杂。如果您经常处理日期和时间数据,它们绝对值得学习。
    猜你喜欢
    • 1970-01-01
    • 2020-10-28
    • 1970-01-01
    • 2012-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多