[英]How to change the (number of) x-axis ticks in a plot.zoo plot? (axis() fails)
[英]R: How do you put the month - year on the X axis of a plot.zoo multiple plot?
在一個閃亮的應用程序中,我想輸入plot.zoo多線圖的月份-年(即“ Jan-13”,“ Feb-13”)。 我已經用谷歌搜索,看起來您必須使用面板和if語句來找到最后一個圖,但是我仍然無法獲得月份-年在x軸上顯示。
這是您可以運行的代碼:
library("zoo")
Factors <- matrix(seq(from=1, to=9, by=1), nrow=3,ncol=3)
Factors
datesNumeric <- cbind(20130101, 20130220,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
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(as.zoo(Factors), main="Factors 1,2, & 3", ylab=c("Factor 1","Factor 2","Factor 3") , xlab= "Date", panel = my.panel,yax.flip=FALSE,col=1:3,xaxt="n")
是否知道如何獲取日期(即“ Jan-13”,“ Feb-13”等)以顯示在x軸上? 謝謝。
您應該創建一個可復制的示例。 plot.zoo
只是一個基本情節。 因此,要使用日期格式化軸,應使用axis.Date
。
x.Date <- as.Date(paste(2003, c(1, 3, 7, 9, 14), 02,sep = "-"))
x <- zoo(rnorm(5), x.Date)
plot(x,xaxt="n")
axis.Date(1, at = x.Date, format= "%m-%y", las = 1)
操作更新后進行編輯 :
您應該創建一個有效的Zoo對象。
plot(zoo(x=Factors,order.by=ticks), ## here your error
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') ## format labels
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.