更改时间序列图上的轴

布莱恩亚当斯

我从以下数据帧制作了一个时间序列。

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

在此处输入图片说明

如何将月份添加到 x 轴?

卡米尔

由于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 包(v0.2.0)2018年 6 月 13 日创建

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章