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

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

问题描述

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

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.

例如

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

为您提供了观察到的时间序列,趋势等的漂亮基本图.使用我自己的数据(水面以下的深度变化),但是,我希望能够切换y的方向轴(例如,对于观察"为ylim = c(40,0),对于趋势"为ylim = c(18,12)),将季节性"更改为潮汐",包括x轴的单位(时间" (天)"),并为该图提供更具描述性的标题.

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.

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

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?

谢谢!皮特

推荐答案

您可以修改plot.decomposed.ts函数(即在类plot时分派的plot方法" >(这是td的类).

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

这是该图的ggplot版本. ggplot需要一个数据帧,因此第一步是将分解后的时间序列转换为数据帧形式,然后对其进行绘制.

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天全站免登陆