在时间序列图上更改轴

时间:2018-06-13 15:02:24

标签: r ggplot2 forecast

我已根据以下数据框制作了时间序列。

     Year    Month Demand
1 2010  January   48.5
2 2010 February   46.0
3 2010    March   54.4
4 2010    April   49.8
5 2010      May   48.1
6 2010     June   55.0

我使用以下命令来制作ts对象:

   ts.Monthly.Demand=Monthly.Demand%>%
  select(Demand)%>%
  ts(start=2010,frequency=12)

我使用以下内容制作情节:

ts.Monthly.Demand%>%
  autoplot()

enter image description here

如何将月份添加到x轴?

2 个答案:

答案 0 :(得分:1)

转换为动物园并使用scale_x_yearmon

library(zoo)

z.Monthly.Demand <- as.zoo(ts.Monthly.Demand)
autoplot(z.Monthly.Demand) + scale_x_yearmon() + xlab("")

,并提供:

screenshot

或使用经典图形:

plot(z.Monthly.Demand)

screenshot

答案 1 :(得分:0)

由于autoplot会返回ggplot个对象,因此您可以像在任何其他ggplot工作流程中一样添加其他ggplot个函数。这包括设置比例,例如使用scale_x_date并根据需要给出日期分隔符。 date_labels的几个格式选项:

library(tidyverse)
library(ggfortify)

ts1 <- df %>%
  select(Demand) %>%
  ts(start = 2010, frequency = 12)

autoplot(ts1) + scale_x_date(date_labels = "%m-%Y")

autoplot(ts1) + scale_x_date(date_labels = "%B %Y")

autoplot(ts1) + scale_x_date(date_labels = "%b '%y")

reprex package(v0.2.0)创建于2018-06-13。

相关问题