【问题标题】:R show month on x axis of a multiple zoo plotR在多个动物园图的x轴上显示月份
【发布时间】:2013-12-11 22:40:50
【问题描述】:

如何修改此代码,以便在 x 上看到每个月的“Jan-10, Feb-10...Jan-11, Mar-11...Jan-13..Dec-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') 

有什么想法吗?

【问题讨论】:

    标签: r


    【解决方案1】:

    试试这个:

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

    【讨论】:

    • @GGrothendieck 谢谢,但您的图表显示 1 月、3 月、5 月......我希望每个月都显示在 x 轴上:1 月 11 日、2 月 11 日、3 月 11 日, Apr-11, ...Dec-11, Jan-12, Feb-12...Dec-12, Jan-13...等...
    • @user3022875,那只是因为它不适合它们。如果你把标签变小,你会得到它们。尝试将cex.axis = 0.7 添加到axis 语句中。
    猜你喜欢
    • 2013-05-18
    • 2020-05-12
    • 1970-01-01
    • 2011-11-10
    • 1970-01-01
    • 2021-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多