【问题标题】:How do I show actual dates in a daily time series in R?如何在 R 中显示每日时间序列中的实际日期?
【发布时间】:2019-12-20 05:59:18
【问题描述】:

我创建了一个每日时间序列。原始数据:

> head(platinum)
        Date Hong.Kong.8.30
1 2019-12-19            938
2 2019-12-18            929
3 2019-12-17            933
4 2019-12-16            931
5 2019-12-13            947
6 2019-12-12            943

我使用了zoo 包,它仍然显示日期:

> plat2=read.zoo(platinum)
> tail(plat2,30)
2019-11-08 2019-11-11 2019-11-12 2019-11-13 2019-11-14 2019-11-15 
       913        894        880        876        878        885 
2019-11-18 2019-11-19 2019-11-20 2019-11-21 2019-11-22 2019-11-25 
       896        899        915        923        917        897 

然而,在as.ts 之后,日期存储为整数:

> as.ts(plat2)
Time Series:
Start = 8217 
End = 18249 
Frequency = 1 

情节也是整数: time series plot

我希望 x 轴以十年为间隔。任何帮助表示赞赏。

【问题讨论】:

  • 可运行代码来重现数据的 sn-p 对快速测试问题非常有帮助。

标签: r date time-series


【解决方案1】:

您真的需要将其转换为 ts 以进行绘图吗? 从zoo 转换中绘制它似乎很好。

如果您需要特定的轴,则可以使用axis() 函数,如下所示。

# make dummy data
df <- data.frame(date = seq(as.Date("1980-01-01"), 
                            as.Date("2019-01-01"), by = "month"),
                 val = rnorm(469))

# read w/ zoo
df_zoo <- read.zoo(df)

# default plot
plot(df_zoo, main = "zoo plot")

# custom axis plot
plot(df_zoo, xaxt = "n", main = "zoo plot w/ modified axis")

tks <- seq(min(df$date), max(df$date), by = "10 years")
axis(1, at = tks, labels = tks, cex.axis = .7)

地块:

【讨论】:

    猜你喜欢
    • 2019-04-24
    • 1970-01-01
    • 1970-01-01
    • 2012-06-08
    • 2016-01-12
    • 1970-01-01
    • 1970-01-01
    • 2020-11-22
    • 1970-01-01
    相关资源
    最近更新 更多