简体   繁体   English

R在多个动物园图的x轴上显示月份

[英]R show month on x axis of a multiple zoo plot

How do I modify this code so that I see every month-year "Jan-10, Feb-10...Jan-11, Mar-11...Jan-13..Dec-13" on the x-axis? 如何修改此代码,以便在x轴上看到每个月的年份“ Jan-10,Feb-10 ... Jan-11,Mar-11 ... Jan-13..Dec-13”?

Right now I only see the first month of the year. 现在,我只看到一年的第一个月。

Here is the code you can run: 这是您可以运行的代码:

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

Any ideas? 有任何想法吗?

Try this: 尝试这个:

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

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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