【问题标题】:how to format x-label for time series graph?如何为时间序列图格式化 x-label?
【发布时间】:2018-07-21 12:48:57
【问题描述】:

这是我的数据框,我从这个数据框中绘制时间序列图

data.frame(df3)

Time                 Price
2/21/2018 09:00:00am 122.12
2/21/2018 09:07:38am 122.43
2/21/2018 09:09:10am 122.44
2/21/2018 09:09:10am 122.45
2/21/2018 09:09:21am 122.26
2/21/2018 09:13:16am 122.37

...

这是时间序列图的编码:

ts.plot(df3$Price, gpars=list(xlab="Time", ylab="Price"))

输出是:

我想更改图表上的 x-label,我想要 x-label 显示的格式 "2/21/2018 09:13:16am " 格式

提前致谢

【问题讨论】:

标签: r time-series


【解决方案1】:

“我想要我的 x-label 显示格式“2/21/2018 09:13:16am”格式”我不确定你的意思。我假设您需要时间序列数据的合理时间点的刻度和标签。

我建议使用zoo。

# Load sample data
df <- read.table(text =
    "Time                 Price
    '2/21/2018 09:00:00am' 122.12
    '2/21/2018 09:07:38am' 122.43
    '2/21/2018 09:09:10am' 122.44
    '2/21/2018 09:09:10am' 122.45
    '2/21/2018 09:09:21am' 122.26
    '2/21/2018 09:13:16am' 122.37", header = T)

# Convert data.frame to zoo time series object
library(zoo)
Price <- zoo(df$Price, as.POSIXct(df$Time, format = "%m/%d/%Y %I:%M:%S%p"))

# Plot
library(ggplot2)
autoplot(Price) + labs(x = "Time")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-27
    • 1970-01-01
    • 2017-09-30
    • 1970-01-01
    • 1970-01-01
    • 2020-11-06
    • 2016-03-11
    相关资源
    最近更新 更多