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: (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.
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)