简体   繁体   中英

how to create a plot with continuous days of year of subsequent years as x axis in R?

I am trying to plot temperature data of two consecutive years (say 05 Nov, 2010 to 30 March, 2011) having days of year as x axis values. For example:

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

please help me out. thanks.

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)

Alternatively:

The most rigorous way of approaching this would be to use the date-time objects within R. Then R will recognise the 'temp' data as dates, and will therefore give the right result when plotted. Date time objects are complicated, but if you deal with them regularly, they are worth learning:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM