如何使用ggfortify库在时间序列数据上实现构面网格功能? [英] How to implement the facet grid feature using the ggfortify library on a time series data?

查看:63
本文介绍了如何使用ggfortify库在时间序列数据上实现构面网格功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 RStudio ,并且有一个名为 data1 时间序列数据( ts 对象)./p>

这是 data1 的外观:

 一月二月三月四月五月五月六月七月八月九月十月十一月十二月2014 135 172 179 189 212 47 301 183 247 292 280 3252015 471 243 386 235 388 257 344 526 363 261 189 1732016 272 267 197 217 393 299 343 341 315 305 384 497 

为绘制以上内容,我运行了以下代码:

 图(data1) 

我得到以下情节:

我想要一个按Year细分的图,我正在考虑实现在 ggplot2 中找到的 facet_grid 功能,但是由于我的数据是 ts对象,我不能直接在其上使用 ggplot2 .

经过研究,我发现 ggfortify 库可用于 ts 对象.但是,我很难确定要使用 facet_grid 功能.

我的目标是从我的 ts 数据中绘制出类似下面的内容:

女性"和男性"将分别替换为2014、2015和2016年.X轴为月份(一月,二月,三月等),y轴为值在 ts 文件中.我更喜欢线图而不是点图.

我是在这里吗?还是有另一种方法来解决这个问题?

解决方案

我们可以使用 ggplot2 :: autoplot .我将以 AirPassengers 数据为例.

 库(ggplot2)库(润滑)自动绘图(AirPassengers)+facet_grid(.〜year(Index),scales ="free_x")+scale_x_date(date_labels =%b") 

I am using RStudio and I have a time series data (ts object) called data1.

Here is how data1 looks:

     Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
2014 135 172 179 189 212  47 301 183 247 292 280 325
2015 471 243 386 235 388 257 344 526 363 261 189 173
2016 272 267 197 217 393 299 343 341 315 305 384 497

To plot the above, I have run this code:

plot (data1)

and I get the following plot:

I want to have a plot that is broken by Year and I was thinking of implementing the facet_grid feature found in ggplot2 but since my data is a ts object, I can't use ggplot2 directly on it.

After some research, I've found that the ggfortify library works with ts objects. However, I am having a hard time trying to figure out to use the facet_grid feature with it.

My aim to is to plot something like below from my ts data:

'Female'and 'Male' will be replaced by the Years 2014, 2015 and 2016. The X-axis will be the Months (Jan, Feb, Mar, and so on) and the y-axis will be the values in the ts file . I would prefer a line plot rather than a dot plot.

Am I on the right track here or is there another way of approaching this problem?

解决方案

We can use ggplot2::autoplot. I will use AirPassengers data as an example.

library(ggplot2)
library(lubridate)
autoplot(AirPassengers) + 
 facet_grid(. ~ year(Index), scales = "free_x") + 
 scale_x_date(date_labels = "%b")

这篇关于如何使用ggfortify库在时间序列数据上实现构面网格功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆