2011-09-26 41 views
0

我想創建一個簡單的圖。如何擺脫x軸上的小數位

year=c(2005,2006,2007) 
dat=c(1,2,3) 
plot(year,dat) 

如何僅將軸顯示爲沒有小數位的年份? 謝謝

回答

1

指定year作爲日期,使用as.Date。這裏有一種方法,使用seq.Date

year=seq(as.Date("2005/01/01"), by="1 year", length.out=3) 
dat=c(1,2,3) 
plot(year,dat) 

enter image description here

0

3種可能的方式:

  • xaxp控制標籤分辨率:

    plot(year,dat,xaxp=c(range(year),2))

  • 使用對於x軸的Date對象:

    year2 <- as.Date(paste(year,"-01-01",sep=""))

    plot(year2,dat)

  • 構建軸自己:

    plot(year,dat,xaxt="n")

    axis(1,at=year)