R如何创建带有日期的自定义x轴

R如何创建带有日期的自定义x轴,r,plot,R,Plot,我想画一些价格和日期的对比图。大约有10万行数据。日期范围为2008年1月7日至2011年1月12日。我希望能够为每年创建一个带有以下记号的自定义x轴: 1) 在1/1具有YYYY(即2011) 2) 在4/1有月份的缩写(即Apr) 3) 在7/1时,使用月份的缩写(即Jul) 4) 在10月1日有月份的缩写(即Oct) 以下是对数据的描述: > head(as.POSIXct(rs4p[,3])) [1] "2008-06-30 20:00:00 EDT" "2008-06-30 20

我想画一些价格和日期的对比图。大约有10万行数据。日期范围为2008年1月7日至2011年1月12日。我希望能够为每年创建一个带有以下记号的自定义x轴:

1) 在1/1具有YYYY(即
2011

2) 在4/1有月份的缩写(即
Apr

3) 在7/1时,使用月份的缩写(即
Jul

4) 在10月1日有月份的缩写(即
Oct

以下是对数据的描述:

> 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"
更新:


另一件我忘记的事情是,当图形绘制完成时,我想在绘图上放置一个
grid()
。有没有办法使网格线与年份记号相对应?

您可以使用
axes=FALSE
抑制默认轴,并通过调用
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"))
#示例数据
图书馆(quantmod)
getSymbols(“^DJI”)

文森特赢了我,但我基本上是这样做的:

# 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)
#您希望采用年份格式的日期

fmtasY文森特先拿到了,但这是我和动物园的版本

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"))

根据更新中的要求添加了网格线代码。非常好。非常感谢你!
grid(nx=NA, ny=NULL)
abline(v=c(years, other.quarters),col = "lightgray", lty = "dotted", lwd = par("lwd"))