如何摆脱x轴上的十进制数字

时间:2011-09-26 14:37:31

标签: r

我想创建一个简单的情节。

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

我怎样才能将轴显示为没有小数位数的年份? 感谢

2 个答案:

答案 0 :(得分:1)

使用yearas.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

答案 1 :(得分: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)