如何在分解的时间序列图中自定义标题、轴标签等 [英] How to customize title, axis labels, etc. in a plot of a decomposed time series

查看:12
本文介绍了如何在分解的时间序列图中自定义标题、轴标签等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相当熟悉通过编写自己的 x 轴标签或主标题来修改绘图的常用方法,但在绘制时间序列分解的结果时,我无法自定义输出.

例如,

库(TTR)t <- ts(co2, 频率=12, 开始=1, deltat=1/12)td <- 分解(t)情节(td)plot(td, main="Title doesn't Work") # 给你一个错误信息

为您提供观察到的时间序列、趋势等的漂亮基本图.但是,使用我自己的数据(水面以下的深度变化),我希望能够切换 y 的方向轴(例如 ylim=c(40,0) 表示 'observed',或 ylim=c(18,12) 表示 'trend'),将 'seasonal' 更改为 'tidal',包括 x 轴的单位('Time(days)'),并为该图提供更具描述性的标题.

我的印象是我正在做的那种时间序列分析是非常基本的,最终,我可能最好使用另一个包,也许有更好的图形控制,但我想使用 ts() 和decompose() 如果我现在可以的话(是的,蛋糕和消费).假设这不会变得太可怕.

有没有办法做到这一点?

谢谢!皮特

解决方案

可以修改plot.decomposed.ts函数(也就是plot方法"得到当您在类 decomposed.ts 的对象(即 td 的类)上运行 plot 时调度.

getAnywhere(plot.decomposed.ts)

<块引用>

函数 (x, ...){xx <- x$xif (is.null(xx))xx <- with(x, if (type == "additive")随机 + 趋势 + 季节性否则随机 * 趋势 * 季节性)情节(cbind(观察= xx,趋势= x$趋势,季节性= x$季节性,随机= x$随机),main = paste("分解", x$type, "时间序列"), ...)}

请注意,在上面的代码中,该函数对标题进行了硬编码.所以让我们修改它,以便我们可以选择自己的标题:

my_plot.decomposed.ts = function(x, title="", ...) {xx <- x$xif (is.null(xx))xx <- with(x, if (type == "additive")随机 + 趋势 + 季节性否则随机 * 趋势 * 季节性)情节(cbind(观察= xx,趋势= x$趋势,季节性= x$季节性,随机= x$随机),主要=标题,...)}my_plot.decomposed.ts(td, "我的标题")

这是情节的 ggplot 版本.ggplot需要数据框,所以第一步是将分解后的时间序列变成数据框形式,然后绘制出来.

library(tidyverse) # 包含 ggplot2 和 tidyr 包,我们在下面使用# 获取时间序列的时间值时间 = 属性(co2)[[1]]Time = seq(Time[1],Time[2], length.out=(Time[2]-Time[1])*Time[3])# 将 td 转换为数据框dat = cbind(时间, with(td, data.frame(Observed=x, Trend=trend, Seasonal=seasonal, Random=random)))ggplot(gather(dat, component, value, -Time), aes(Time, value)) +facet_grid(component ~ ., scales="free_y") +geom_line() +主题_bw() +实验室(y=表达式(CO[2]~(ppm)), x="Year") +ggtitle(表达式(分解~CO[2]~时间~系列)) +主题(plot.title=element_text(hjust=0.5))

I'm reasonably familiar with the usual ways of modifying a plot by writing your own x axis labels or a main title, but I've been unable to customize the output when plotting the results of a time series decomposition.

For example,

library(TTR)
t <- ts(co2, frequency=12, start=1, deltat=1/12)
td <- decompose(t)
plot(td)
plot(td, main="Title Doesn't Work") # gets you an error message

gives you a nice, basic plot of the observed time series, trend, etc. With my own data (changes in depth below the water surface), however, I'd like to be able to switch the orientation of the y axes (eg ylim=c(40,0) for 'observed', or ylim=c(18,12) for 'trend'), change 'seasonal' to 'tidal', include the units for the x axis ('Time (days)'), and provide a more descriptive title for the figure.

My impression is that the kind of time series analyses I'm doing is pretty basic and, eventually, I may be better off using another package, perhaps with better graphical control, but I'd like to use ts() and decompose() if I can for now (yeah, cake and consumption). Assuming this doesn't get too horrendous.

Is there a way to do this?

Thanks! Pete

解决方案

You can modify the plot.decomposed.ts function (that's the plot "method" that gets dispatched when you run plot on an object of class decomposed.ts (which is the class of td).

getAnywhere(plot.decomposed.ts)

function (x, ...) 
{
    xx <- x$x
    if (is.null(xx)) 
        xx <- with(x, if (type == "additive") 
            random + trend + seasonal
        else random * trend * seasonal)
    plot(cbind(observed = xx, trend = x$trend, seasonal = x$seasonal, random = x$random), 
         main = paste("Decomposition of", x$type, "time series"), ...)
}

Notice in the code above that the function hard-codes the title. So let's modify it so that we can choose our own title:

my_plot.decomposed.ts = function(x, title="", ...) {
  xx <- x$x
  if (is.null(xx)) 
    xx <- with(x, if (type == "additive") 
      random + trend + seasonal
      else random * trend * seasonal)
  plot(cbind(observed = xx, trend = x$trend, seasonal = x$seasonal, random = x$random), 
       main=title, ...)
}

my_plot.decomposed.ts(td, "My Title")

Here's a ggplot version of the plot. ggplot requires a data frame, so the first step is to get the decomposed time series into data frame form and then plot it.

library(tidyverse) # Includes the packages ggplot2 and tidyr, which we use below

# Get the time values for the time series
Time = attributes(co2)[[1]]
Time = seq(Time[1],Time[2], length.out=(Time[2]-Time[1])*Time[3])

# Convert td to data frame
dat = cbind(Time, with(td, data.frame(Observed=x, Trend=trend, Seasonal=seasonal, Random=random)))

ggplot(gather(dat, component, value, -Time), aes(Time, value)) +
  facet_grid(component ~ ., scales="free_y") +
  geom_line() +
  theme_bw() +
  labs(y=expression(CO[2]~(ppm)), x="Year") +
  ggtitle(expression(Decomposed~CO[2]~Time~Series)) +
  theme(plot.title=element_text(hjust=0.5))

这篇关于如何在分解的时间序列图中自定义标题、轴标签等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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