如何在分解的时间序列图中自定义标题、轴标签等
How to customize title, axis labels, etc. in a plot of a decomposed time series
我相当熟悉通过编写您自己的 x 轴标签或主标题来修改绘图的常用方法,但是在绘制时间序列分解的结果时我无法自定义输出。
例如,
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 轴(例如 'observed' 的 ylim=c(40,0) 或 'trend' 的 ylim=c(18,12)),将 'seasonal' 更改为 'tidal',包括 x 轴的单位 ('Time (days)'),并为图形提供更具描述性的标题。
我的印象是我正在做的时间序列分析是非常基础的,最终,我可能最好使用另一个包,也许有更好的图形控制,但我想使用 ts( ) 和 decompose() 如果我现在可以的话(是的,蛋糕和消费)。假设这不会太可怕。
有办法吗?
谢谢!皮特
您可以修改 plot.decomposed.ts
函数(即当您 运行 plot
在 object 上调度的 plot
"method" class decomposed.ts
(即 td
的 class)。
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"), ...)
}
注意上面代码中函数 hard-codes 的标题。所以让我们修改它,以便我们可以选择我们自己的标题:
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需要一个数据框,所以第一步是把分解的时间序列变成数据框的形式,然后画出来。
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))
我相当熟悉通过编写您自己的 x 轴标签或主标题来修改绘图的常用方法,但是在绘制时间序列分解的结果时我无法自定义输出。
例如,
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 轴(例如 'observed' 的 ylim=c(40,0) 或 'trend' 的 ylim=c(18,12)),将 'seasonal' 更改为 'tidal',包括 x 轴的单位 ('Time (days)'),并为图形提供更具描述性的标题。
我的印象是我正在做的时间序列分析是非常基础的,最终,我可能最好使用另一个包,也许有更好的图形控制,但我想使用 ts( ) 和 decompose() 如果我现在可以的话(是的,蛋糕和消费)。假设这不会太可怕。
有办法吗?
谢谢!皮特
您可以修改 plot.decomposed.ts
函数(即当您 运行 plot
在 object 上调度的 plot
"method" class decomposed.ts
(即 td
的 class)。
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"), ...) }
注意上面代码中函数 hard-codes 的标题。所以让我们修改它,以便我们可以选择我们自己的标题:
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需要一个数据框,所以第一步是把分解的时间序列变成数据框的形式,然后画出来。
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))