如何替换情节中的标题

How to replace a title in a plot

我从这个 link 中提取了一个时间序列,我使用这些命令来分隔时间序列的组成部分:

pil.us <- read.csv("./Datasets/GDP.csv", sep = ",")
xt <- ts(pil.us$GDP, start=(1947,1), freq=4)
d.pil <- decompose(xt, type="additive")

现在,我想做的是将这些组件绘制在一起并更改绘图的标题,我试过了

plot(d.pil, main="Decomposizione della serie storica PIL US")

但它给了我错误 Formal argument "main" matched by multiple actual arguments 我认为这是因为 d.pil 是一个数据帧。

我也试过这个:

plot(d.pil)
title(main="Decomposizione della serie storica PIL US")

我明白了这个情节,但如您所见,新标题并没有取代旧标题。

如何获得只有一个标题的情节?

您可以破解 stats:::plot.decomposed.ts 方法并添加一个(可扩展的)字典以及一个 add2main 组件。

plot.decomposed.ts <- function (x, lang=1L, add2main=NULL, ...) {
  main <- matrix(c("Decomposition of", "Decomposizione della", 
                   "time series", "serie storica"), 2L)[lang, ]
  xx <- stats:::`%||%`(x$x, 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(main[1L], 
                                          x$type, main[2L], add2main), ...)
}

plot(decompose(x), lang=2L, add2main='PIL US')  ## 1L for EN, 2L for IT


数据:

x <- structure(c(-50, 175, 149, 214, 247, 237, 225, 329, 729, 809, 
530, 489, 540, 457, 195, 176, 337, 239, 128, 102, 232, 429, 3, 
98, 43, -141, -77, -13, 125, 361, -45, 184), .Tsp = c(1951, 1958.75, 
4), class = "ts")