2013-12-16 3 views
7
x=read.table(text=" Qtr1 Qtr2 Qtr3 Qtr4 
2010 1.8 8.0 6.0 3.0 
2011 2.0 11.0 7.0 3.5 
2012 2.5 14.0 8.0 4.2 
2013 3.0 15.2 9.5 5.0", 
    sep="",header=TRUE) 
y<-ts(as.vector(as.matrix(x)),frequency=4,start=c(2010,1)) 
plot.ts(y) 
time<-seq(as.Date("2010/1/1"),length.out=20,by="3 months") 
axis(1, at = time) 

음모를 꾸미고, 난 내 axis(1, at = time)는 x 축에 날짜 데이터를 추가 할 수없는 이유를 x 축에 날짜를 추가하려면?설정 x 축 라벨 내가 그래프를 그릴 때 시계열

답변

6

axis(1, at=time)을 호출하면 time에 지정된 점에 레이블이있는 x 축을 그려 넣으려고합니다. 그러나 time은 숫자가 아닌 문자의 벡터입니다.

일반적으로 axis(1, at=..., labels=...)을 호출하면 실제 레이블과 축을 따라 레이블을 배치 할 위치를 나타냅니다. 귀하의 경우, plot.ts 전화는 x 축 한계를 20102013.75으로 암시 적으로 설정하므로 at 매개 변수에 이러한 제한이 반영되어야합니다.

axis을 호출하면 레이블은 time이고 위치는 2010, 2010.25, 2010.50 ..., 즉 seq(from=2010, to=2013.25, by=0.25)입니다. 일반적인 해결책은 다음과 같습니다.

plot.ts(y,axes=F) # don't plot the axes yet 
axis(2) # plot the y axis 
axis(1, labels=time, at=seq(from=2010, by=0.25, length.out=length(time))) 
box() # and the box around the plot 
관련 문제