More x-tick labels on an Acf plot

Hi all,

I use an ACF plot and I would like to have more x-ticks. So I plot 200 lags but the x-tick labels are only every 50th leg, so at 0, 50, 100, 150, 200. I'd like to have a x-tick label after 20 legs: 0, 20, 40, 60, ..., 200

Here is the code that I use

library(forecast)
Acf(electricityPrices2019, lag.max = 200)

I'd appreciate every comment and would be quite thankful for your help.

library(fpp2)
#> Registered S3 method overwritten by 'quantmod':
#>   method            from
#>   as.zoo.data.frame zoo
#> ── Attaching packages ────────────────────────────────────────────── fpp2 2.4 ──
#> βœ“ ggplot2   3.3.2     βœ“ fma       2.4  
#> βœ“ forecast  8.13      βœ“ expsmooth 2.3
#> 
z4  <- ts(matrix(rnorm(400), 100, 4), start = c(1961, 1), frequency = 12)
ggAcf(z4) + 
  scale_x_continuous(breaks = c(4,8,12,16,18,22)) + 
  theme_minimal()
#> Scale for 'x' is already present. Adding another scale for 'x', which will
#> replace the existing scale.

Created on 2020-10-28 by the reprex package (v0.3.0.9001)

Thanks technocrat for your answer,

maybe I did not explain precisely what I want. I do not want to have multiple series in the ACF plot. I just have one ACF plot and the x-axis ticks (so the labels on the axis) should be adjusted. I uploaded a screenshot with the desired output (in red)

2 Likes

Same way with your breaks


scale_x_continuous(breaks = c(4,8,12,16,18,22))

Thanks for your answer technocrat and your effort,
When I use the statement I get an error message:
library(fpp2)
Error in library(fpp2) : es gibt kein Paket namens β€˜fpp2’
Translation: There is not package named 'fpp2'

I also used the statement

library(forecast)
Acf(generationData$Price[0:200], scale_x_continuous(breaks = c(4,8,12,16,18,22)))

Here I get the following error message:

Error in scale_x_continuous(breaks = c(4, 8, 12, 16, 18, 22)) :
could not find function "scale_x_continuous"

1 Like
install.packages(β€œfpp2”)
install.packages(β€œggplot2”)

install.packages(β€œfpp2”)
Error: unexpected input in "install.packages("

Curly quotation marks won't work; they must be straight.

op <- options("useFancyQuotes")
paste("argument", sQuote("x"), "must be non-zero")
#> [1] "argument 'x' must be non-zero"
options(useFancyQuotes = FALSE)
cat("\ndistinguish plain", sQuote("single"), "and",
    dQuote("double"), "quotes\n")
#> 
#> distinguish plain 'single' and "double" quotes
options(useFancyQuotes = TRUE)
cat("\ndistinguish fancy", sQuote("single"), "and",
    dQuote("double"), "quotes\n")
#> 
#> distinguish fancy β€˜single’ and β€œdouble” quotes
options(useFancyQuotes = "TeX")
cat("\ndistinguish TeX", sQuote("single"), "and",
    dQuote("double"), "quotes\n")
#> 
#> distinguish TeX `single' and ``double'' quotes
if(l10n_info()$`Latin-1`) {
  options(useFancyQuotes = c("\xab", "\xbb", "\xbf", "?"))
  cat("\n", sQuote("guillemet"), "and",
      dQuote("Spanish question"), "styles\n")
} else if(l10n_info()$`UTF-8`) {
  options(useFancyQuotes = c("\xc2\xab", "\xc2\xbb", "\xc2\xbf", "?"))
  cat("\n", sQuote("guillemet"), "and",
      dQuote("Spanish question"), "styles\n")
}
#> 
#>  Β«guillemetΒ» and ΒΏSpanish question? styles
options(op)

Created on 2020-10-30 by the reprex package (v0.3.0.9001)

Thanks technocrat for your answer and effort,

now I managed to install the fpp2 and ggplot2 packages but unfortunately I still get the same error as posted above when using the code:

Acf(generationData$Price[0:200], scale_x_continuous(breaks = c(4,8,12,16,18,22)))
Error in scale_x_continuous(breaks = c(4, 8, 12, 16, 18, 22)) :
could not find function "scale_x_continuous"

library(fpp2)
#> Registered S3 method overwritten by 'quantmod':
#>   method            from
#>   as.zoo.data.frame zoo
#> ── Attaching packages ────────────────────────────────────────────── fpp2 2.4 ──
#> βœ“ ggplot2   3.3.2     βœ“ fma       2.4  
#> βœ“ forecast  8.13      βœ“ expsmooth 2.3
#> 
Acf(wineind) 
autoplot(Acf(wineind)) + 
  scale_x_continuous(breaks = c(4, 8, 12, 16, 18, 22)) + 
  theme_minimal()
#> Scale for 'x' is already present. Adding another scale for 'x', which will
#> replace the existing scale.

Created on 2020-11-02 by the reprex package (v0.3.0.9001)

Hi, I cannot fully reproduce your example, but in the meanwhile while you work Richard's (@technocrat) solution, you can go back to base:

This is a similar example :slight_smile:

## load a time series
> data(lynx)
> par(mfrow = c(2,1))
> forecast::Acf(lynx, lag.max = 200)
> acf(lynx, lag.max = 200, xaxt = 'no')
> axis(1, at = seq(0,200,10))

lynx1

Plots are the same from forecast::Acf and stats:acf, only changing at the axis tick level. I wonder why forecast::Acf does not support straigth xaxt = 'no' but you could circunvent also by adding the extra axis values in a custom way to your specific extra ticks needed:

par(mfrow = c(2,1))
forecast::Acf(lynx, lag.max = 200)
forecast::Acf(lynx, lag.max = 200)
axis(1, at = seq(10, 200, 20))

lynx2

Thanks technocrat and Fer for your help and effort,

basically the solution from autocrat works. Thanks for your answer. Is it also possible to use plot instead of autoplot? The plot command seems to be easy to use and is not as complex as autoplot or ggplot for beginners.

Hi back. Sorry, not sure what you mean. I have never use autoplot, and barely ggplot functions. acf should take almost any argument that plot takes.
In fact,

data(lynx)
acf(lynx)

and

data(lynx)
L <- acf(lynx, plot = FALSE)
plot(L)

Should give you the same graphical output

cheers
F.

Thanks Fer for your answer,

I'd like to use plot but still have customized x-ticks. Is this possible? In your example there are no customized x-ticks whereas in the code of technocrat the x-ticks are customized:

Acf(ts(generationData$Price))
autoplot(Acf(ts(generationData$Price))) +
+     scale_x_continuous(breaks = c(4, 8, 12, 16, 18, 22, 26, 28)) +
+     theme_minimal()

Hi back.
I expand a bit more my last comment, but maybe you should stick to Richard's example with autoplot. Here I was offering an alternative to what he did :slight_smile:

This code was to show that an object from acf can be plotted via plot, and thus, accept all the extra tunning from pars...

So in the example below, with xaxt = 'no' you tell R to not plot the x axis ticks

and then axis(1, at = c(1,5,10,50,75)) yo draw the ticks in the specific locations you want as they are written inside at = c(your ticklocations). The 1 means you want to draw an axis on the X bar....

data(lynx)
L <- acf(lynx, plot = FALSE)
plot(L, xaxt = 'no')
axis(1, at = c(1,5,10,50,75))

cheers
Fer

1 Like

Thanks a lot Fer for your answer and great help. I really appreciate it. Now your code with plot works fine and I think I am going to use this (and not the autoplot).

Also a big thank you to technocrat for your help and effort. Your solution is also quite valuable.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.