R在多动物园情节的x轴上显示月份

时间:2013-12-11 22:40:51

标签: r

我如何修改此代码,以便在x上看到每个月的“1月10日,2月10日... 1月11日,3月11日... 1月13日... 12月13日”轴?

现在我只看到一年中的第一个月。

以下是您可以运行的代码:

library("zoo")

Factors <- matrix(seq(from=1, to=9, by=1), nrow=3,ncol=3)
Factors
datesNumeric <- cbind(20110101, 20120220,20130801)
dates <- as.Date(as.character(datesNumeric), format="%Y%m%d")
ticks <- seq(dates[1], dates[length(dates)], by = "1 month") #I make some ticks
ticks

my.panel <- function(x, y, ..., pf = parent.frame()) {
  grid(NA,NULL)
  #abline(v=seq(1,168,24),col = "lightgray", lty = "dotted", lwd = par("lwd"))
  lines(x, y, ...)

  #if bottom panel  --> I don't think I need this if statement...Do I?
  if (with(pf, length(panel.number) == 0 ||panel.number %% nr == 0 || panel.number == nser)) {
    #axis(1, at = ticks, labels = ticks)
   # axis.Date(1, at = ticks, format= "%m-%y", las = 1)
  }
}

plot(zoo(x=Factors,order.by=ticks), main="Factors 1,2, & 3", ylab=c("Factor 1","Factor 2","Factor     3") ,    xlab= "Date", panel = my.panel,yax.flip=FALSE,col=1:3,format='%b-%y') 

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

试试这个:

my.panel <- function(x, y, ..., pf = parent.frame()) {
  grid(NA,NULL)
  lines(x, y, ...)
  if (pf$panel.number == 3) axis(1, at = ticks, labels = format(ticks, "%b-%y"))
}

plot(zoo(Factors, ticks), 
   main = "Factors 1, 2 & 3", xlab= "Date", ylab = paste("Factor", 1:3),
   panel = my.panel,
   col = 1:3,
   xaxt = "n") 

enter image description here