2013-04-03 42 views
2

具体日期strptime我有头选择r中

YEAR MONTH DAY value 

它每小时运行从1/6/2010至14/7/2012一个文本文件中的数据集。我用下面的命令打开和绘制数据:

data=read.table('example.txt',header=T) 
time = strptime(paste(data$DAY,data$MONTH,data$YEAR,sep="-"), format="%d-%m-%Y") 
plot(time,data$value) 

然而,当数据被绘制,x轴仅示出了2011年和2012年enter image description here。我该怎么做才能保留2011年和2012年的标签,并且还要添加一些特定的月份,例如如果我想要三月,六月&九月?

我所做的数据可用此链接

https://dl.dropbox.com/u/107215263/example.txt

回答

5

你需要为你想使用的功能axis.POSIXct格式化你的日期标签的处理上:

plot(time,data$value,xaxt="n") #Skip the x-axis here 
axis.POSIXct(1, at=pretty(time), format="%B %Y") 

enter image description here

要查看所有可能的格式,请参阅?strptime
你当然也可以用参数at玩把你的蜱无论你想要的,比如:

axis.POSIXct(1, at=seq(time[1],time[length(time)],"3 months"), 
      format="%B %Y") 

enter image description here

+1

你的答案是有点混乱,在第一,因为你使用的是自己的样本数据。也许你可以使用OP的示例数据来清晰。 –

+0

由于这个问题只与X轴设计有关,所以我认为这对任何人都不重要(我也没有看到OP将他的数据叠加)。但这里是OP数据。 – plannapus

0

尽管这并不直接回答的问题,我想建议你使用xts包用于任何时间序列分析。它使时间序列分析非常方便

require(xts) 

DF <- read.table("https://dl.dropbox.com/u/107215263/example.txt", header = TRUE) 
head(DF) 
## YEAR MONTH DAY value 
## 1 2010  6 1 95.3244 
## 2 2010  6 2 95.3817 
## 3 2010  6 3 100.1968 
## 4 2010  6 4 103.8667 
## 5 2010  6 5 104.5969 
## 6 2010  6 6 107.2666 

#Get Index for xts object which we will create in next step 
DFINDEX <- ISOdate(DF$YEAR, DF$MONTH, DF$DAY) 

#Create xts timeseries 
DF.XTS <- .xts(x = DF$value, index = DFINDEX, tzone = "GMT") 


head(DF.XTS) 
##       [,1] 
## 2010-06-01 12:00:00 95.3244 
## 2010-06-02 12:00:00 95.3817 
## 2010-06-03 12:00:00 100.1968 
## 2010-06-04 12:00:00 103.8667 
## 2010-06-05 12:00:00 104.5969 
## 2010-06-06 12:00:00 107.2666 

#plot xts 
plot(DF.XTS) 

enter image description here