fabianod
fabianod

Reputation: 693

How to replace a title in a plot

I have a time series taken from this link, i used these commands to separate the components of the time series:

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

Now, what i want to do is to plot these components together and change the title of the plot, i tried this

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

but it gives me the error Formal argument "main" matched by multiple actual arguments and i think this happens because d.pil is a dataframe.

I also tried this:

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

I get this plot, but as you can see, the new title did not replace the old. Plot

How can i get a plot with only one title?

Upvotes: 2

Views: 644

Answers (1)

jay.sf
jay.sf

Reputation: 72909

You could hack the stats:::plot.decomposed.ts method and add a (expandable) dictionary as well as an add2main component.

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

enter image description here


Data:

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

Upvotes: 1

Related Questions