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)
when i draw the graph ,i want to add date in x axis,why my axis(1, at = time)
can't add the date data in x axis?
当我绘制图形时,我想在x轴上添加日期,为什么我的轴(1,at = time)不能在x轴上添加日期数据?
1 个解决方案
#1
6
When you call axis(1, at=time)
you're telling R to plot the x axis with labels at points given by time
. However, time
is a vector of characters, not numbers.
当你调用axis(1,at = time)时,你告诉R在时间给出的点上用标签绘制x轴。但是,时间是字符的向量,而不是数字。
In general, you call axis(1, at=..., labels=...)
indicating the actual labels and where to place them along the axis. In your case, your call to plot.ts
implicitly sets the x-axis limits to 2010
and 2013.75
, so your at
parameter should reflect those limits.
通常,您调用axis(1,at = ...,labels = ...)来指示实际标签以及沿轴放置它们的位置。在您的情况下,您对plot.ts的调用会隐式地将x轴限制设置为2010和2013.75,因此您的at参数应该反映这些限制。
So you want to call axis
saying that the labels are time
and the positions are 2010, 2010.25, 2010.50 ...
, that is, seq(from=2010, to=2013.25, by=0.25)
. A general solution is this one:
因此,您希望调用轴表示标签是时间,位置是2010,2010.25,2010.50 ...,即seq(从= 2010,到= 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
#1
6
When you call axis(1, at=time)
you're telling R to plot the x axis with labels at points given by time
. However, time
is a vector of characters, not numbers.
当你调用axis(1,at = time)时,你告诉R在时间给出的点上用标签绘制x轴。但是,时间是字符的向量,而不是数字。
In general, you call axis(1, at=..., labels=...)
indicating the actual labels and where to place them along the axis. In your case, your call to plot.ts
implicitly sets the x-axis limits to 2010
and 2013.75
, so your at
parameter should reflect those limits.
通常,您调用axis(1,at = ...,labels = ...)来指示实际标签以及沿轴放置它们的位置。在您的情况下,您对plot.ts的调用会隐式地将x轴限制设置为2010和2013.75,因此您的at参数应该反映这些限制。
So you want to call axis
saying that the labels are time
and the positions are 2010, 2010.25, 2010.50 ...
, that is, seq(from=2010, to=2013.25, by=0.25)
. A general solution is this one:
因此,您希望调用轴表示标签是时间,位置是2010,2010.25,2010.50 ...,即seq(从= 2010,到= 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