R how to create custom x-axis with dates

匿名 (未验证) 提交于 2019-12-03 01:34:02

问题:

I'm trying to plot some prices vs dates. There are about 10k lines of data. The dates are a range from ~ 7/1/2008 - 12/1/2011. I'd like to be able to create a custom x-axis with the following ticks on it for each year:

1) at 1/1 have the YYYY (ie 2011)

2) at 4/1 have the abbreviation of the month (ie Apr)

3) at 7/1 have the abbreviation of the month (ie Jul)

4) at 10/1 have the abbreviation of the month (ie Oct)

Here's a description of the data:

> head(as.POSIXct(rs4p[,3])) [1] "2008-06-30 20:00:00 EDT" "2008-06-30 20:00:00 EDT" [3] "2008-06-30 20:00:00 EDT" "2008-06-30 20:00:00 EDT" [5] "2008-06-30 20:00:00 EDT" "2008-06-30 20:00:00 EDT" > head((rs4p[,3])) [1] "2008-07-01" "2008-07-01" "2008-07-01" "2008-07-01" "2008-07-01" [6] "2008-07-01" > min((rs4p[,3])) [1] "2008-07-01" > max((rs4p[,3])) [1] "2011-12-08" > class((rs4p[,3])) [1] "Date" 

UPDATE:

One other thing I forgot is that when the graph is done I'd like to put a grid() over the plot. Is there a way to make the grid lines correspond to the year ticks?

回答1:

Vincent got it first, but here's my version with zoo

library(zoo) my.ts <-zoo(0:1000,as.Date("2000-01-01")+0:1000) plot(my.ts, xaxt="n")  years <-index(my.ts)[format(index(my.ts),"%m-%d") %in% "01-01"] other.quarters <- index(my.ts)[ format(index(my.ts), "%m-%d") %in% c("04-01", "07-01","10-01")] axis.Date(1, at=years, label=format(years,"%y")) axis.Date(1, at=other.quarters, label=format(other.quarters, "%b")) 

UPDATE: Here's how to add gridlines:

grid(nx=NA, ny=NULL) abline(v=c(years, other.quarters),col = "lightgray", lty = "dotted", lwd = par("lwd")) 



回答2:

You can suppress the default axes with axes=FALSE, and add them by hand by calling axis.Date.

# Sample data library(quantmod) getSymbols( "^DJI" ) x <- DJI  # Plot without the date axis matplot(    index(x), coredata(Ad(x)),    axes=FALSE,    xlab="", ylab="",   type="l", lwd=3 ) axis(2, las=1)  # Some date arithmetics all_days <- seq.Date( from=min(index(x)), to=max(index(x)), by=1 ) months  <- all_days[ format(all_days, "%d") == "01" ] january <- all_days[ format(all_days, "%m-%d") == "01-01" ] april   <- all_days[ format(all_days, "%m-%d") == "04-01" ] july    <- all_days[ format(all_days, "%m-%d") == "07-01" ] october <- all_days[ format(all_days, "%m-%d") == "10-01" ]  # Finally plot the axes axis.Date(1, at=months,  label=FALSE, tcl=-.3)  axis.Date(1, at=january, label=format(january, "%Y")) axis.Date(1, at=april,   label=format(april,   "%b")) axis.Date(1, at=july,    label=format(july,    "%b")) axis.Date(1, at=october, label=format(october, "%b")) 

You may also want to look at ggplot2: the default axis for dates rarely needs tweaking.



回答3:

Vincent beat me to it, but here's how I did basically the same thing:

# Dates you want to have in the year format fmtasY <- "Jan 01"  # Dates you want to have in the abbreviated month format (%b) fmtasb <- c("Apr 01", "Jul 01", "Oct 01")  # Some dates dates <- as.Date(14061:15309, origin = as.Date("1970-01-01"))  # Some intermediate labels for formatting intermlabs <- format(dates, "%b %d")  # Set up final labels yourlabs <- NA  # If the date is Jan 01, the label should be %Y yourlabs[intermlabs %in% fmtasY] <- format(dates[intermlabs %in% fmtasY], "%Y")  # If the date is the first of April, July or October, label with %b yourlabs[intermlabs %in% fmtasb] <- format(dates[intermlabs %in% fmtasb], "%b")  # Check your work data.frame(dates, intermlabs, yourlabs) 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!