【问题标题】:How to format axes in R, year and months如何在 R、年和月中格式化轴
【发布时间】:2009-12-11 09:20:06
【问题描述】:

我有以下数据集

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

如何在R 中绘制线图,x 轴为年份,每 5 年显示一次?

我的问题不在于制作情节,而在于只显示我想要的年份,并将它们放在那一年的开始。所以我不想在四月打勾,而是在一月。

【问题讨论】:

  • R 是一个统计计算环境。例如,单击 r 标签。我将添加一个指向他们主页的链接。

标签: r plot


【解决方案1】:

这应该让你开始:

# create some data
set.seed(1)
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)

【讨论】:

    【解决方案2】:

    默认情况下使用zoo 建议的rcs(正确!)作为带有线条和相同轴的绘图:

    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 进行子集化处理,也会得到更少的滴答声。

    【讨论】:

      猜你喜欢
      • 2021-12-11
      • 1970-01-01
      • 2018-10-24
      • 2014-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-08
      • 2016-10-23
      相关资源
      最近更新 更多