0

I'm trying to plot a grid for a time series plot using abline(). It works fine, but I'm not able to draw the grid lines in the background: they are above the time series line.

I'm using the following code:

options(repr.plot.width=15, repr.plot.height=10)
par(cex=1.5)

plot(Serie_PIB_NO_ajustados_hasta_2018,
    xlab="Tiempo", #Título de los ejes
    ylab="Miles de millones de €", 
    main="PIB pm Demanda España (datos no ajustados de estacionalidad y calendario)",
    col="blue",
    lwd = 3,
    xlim = c(1995, 2020),
    ylim = c(0, 350),
    xaxp = c(1995, 2019, 8)) 

lines(Serie_PIB_NO_ajustados_2019,
      col="green", lwd=3)

abline(h = c(0, 50, 100, 150, 200, 250, 300, 350), col = "grey")
abline(v = c(1995, 1998, 2001, 2004, 2007, 2010, 2013, 2016, 2019), col = "grey") 

To get this graph: enter image description here (You could see that the grid lines are above the time series lines).

I've tried to solve the problem using panel.first and tck options, as suggested in several related questions. But they don't work for me.

Data may be downloaded from this link.

AlejandroDGR
  • 178
  • 1
  • 10
  • 1
    what kind of object is `Serie_PIB_NO_ajustados_hasta_2018` – rawr Jul 13 '22 at 18:11
  • Does this answer your question? [How do I draw gridlines using abline() that are behind the data?](https://stackoverflow.com/questions/7263743/how-do-i-draw-gridlines-using-abline-that-are-behind-the-data). There are more options than using `panel.first`. If it doesn't work, please update your answer with some data. – harre Jul 13 '22 at 18:18
  • @rawr it's a `ts` object. I've created it using `ts()` function. – AlejandroDGR Jul 13 '22 at 18:20
  • @harre I've already tried that solution, but `panel.first` seems to not work in my case. – AlejandroDGR Jul 13 '22 at 18:21
  • 2
    `plot(ts(1:5), panel.first = grid())` works so theres not much we can do for you without other details – rawr Jul 13 '22 at 18:23
  • @harre I'd rather using `abline()` because `grid()` has problem with adjusting the vertical lines to a time series plot ticks. – AlejandroDGR Jul 13 '22 at 18:23
  • 1
    Please post some data according to https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example and explain more in depth what you have tried. – harre Jul 13 '22 at 18:24
  • @rawr I've already tried `tck` too. It doesn't work either. – AlejandroDGR Jul 13 '22 at 18:27
  • 1
    With base graphics, elements are added in the sequence of the commands. Use a `plot(NA, ...)` command to set up the plot, but not add any lines. Then use the `abline()` (it can take both h= and v= in a single call), then add the two sets of lines with two `lines()` calls. – dcarlson Jul 14 '22 at 03:43
  • @dcarlson Ooohhh this is totally correct. Thanks a lot! You should post it as an answer. – AlejandroDGR Jul 14 '22 at 10:31

1 Answers1

1

This should work:

plot(NA, xlab="Tiempo", #Título de los ejes
    ylab="Miles de millones de €", 
    main="PIB pm Demanda España (datos no ajustados de estacionalidad y calendario)",
    xlim = c(1995, 2020),
    ylim = c(0, 350),
    xaxp = c(1995, 2019, 8)) 

abline(h = c(0, 50, 100, 150, 200, 250, 300, 350), col = "grey")
abline(v = c(1995, 1998, 2001, 2004, 2007, 2010, 2013, 2016, 2019), col = "grey") 

lines(Serie_PIB_NO_ajustados_hasta_2018, col="blue", lwd = 3)
lines(Serie_PIB_NO_ajustados_2019, col="green", lwd=3)
dcarlson
  • 10,936
  • 2
  • 15
  • 18