2009-12-11 30 views
4

格式化軸,我有以下數據集如何在R,一年個月

1890 mar 0.4 
1890 apr 0.8 
1890 may 1.0 
... 
1989 jan 0.2 
1989 feb 0.4 
1989 mar 0.5 

我怎樣才能在x軸的一年做出R線圖使用,展示每5年?

我的問題並不在於製作情節,而是僅僅展示我想要的年份,並將它們放在當年的開始。所以我不想在四月但是一月份打勾。

+5

R是一個統計計算環境。點擊例如r標籤。我會添加一個鏈接到他們的主頁。 – 2009-12-11 09:30:56

回答

4

這應該讓你開始:

# create some data 
tmp <- seq(as.POSIXct("1890-03-01", tz="GMT"), 
      as.POSIXct("1920-03-01", tz="GMT"), 
      by="month") 
df <- data.frame(date=tmp, 
       val=rnorm(length(tmp))) 

# plot data 
plot(df$date, df$val, xaxt="n") 
tickpos <- seq(as.POSIXct("1890-01-01", tz="GMT"), 
       as.POSIXct("1920-01-01", tz="GMT"), 
       by="5 years") 
axis.POSIXct(side=1, at=tickpos) 

alt text http://img704.imageshack.us/img704/9341/axis.png

2

你會得到什麼RCS通過使用zoo與線條圖和同一軸線上默認的建議(正確!):

R> library(zoo) 
R> zdf <- zoo(df$val, order.by=df$date) 
R> plot(zdf) 
R> 

help(plot.zoo)示例顯示了更有趣的日期索引,基本上rcs向您展示了什麼,但是通過附加格式化比如說,

R> fmt <- "%Y-%m" ## year-mon 
R> txt <- format(index(zdf), fmt) 
R> plot(zdf, xaxt='n') 
R> axis(side=1, at=index(zdf), lab=txt) 
R> 

如果子集atlab你會得到更少的蜱太。

相關問題