如何格式化时序图的X-标签?

时间:2018-07-21 03:29:25

标签: r time-series

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

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"))

输出为:

enter image description here

我想更改图形上的x标签,我希望x标签显示的格式"2/21/2018 09:13:16am "格式

预先感谢

1 个答案:

答案 0 :(得分:1)

“我希望我的x标签显示格式为“ 2/21/2018 09:13:16 am” format” 我不确定您的意思。我假设您要在时间序列数据的合理时间点上打勾和标记。

我建议使用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")

enter image description here