2013-04-03 39 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