Valeria Arango
Valeria Arango

Reputation: 327

Add a vertical line to a plot in R

Maybe you can help me. I need to plot a time series, let's call it, ts for that, I use the following code:

plot(ts,col="royalblue", ylab="time series plot", main = "Plot", grid.col=NA)

But I also need to plot in the same plot, a vertical line at the date 18-03-2020 (March 18, 2020). I tried with the following line of code but I don't get any vertical line:

geom_vline(xintercept = 03-18-2020, color="red", linetype="doted", size=2.5)

And also with this one:

abline(a=NULL,b=NULL,h=NULL,v="18-03-2020", col="red")

And this one:

date1 <- as.Date("2020-03-18") + 0:99
abline(v=as.Date(date1))

But with none of them I get the vertical line I need.

What am I doing wrong? could you help me?

Upvotes: 8

Views: 23547

Answers (1)

TarJae
TarJae

Reputation: 78927

You can do it with abline. Set v and position of x axis where you want the horizontal line. In this example v=0.4

plot(ts,col="royalblue", ylab="time series plot", main = "Plot", grid.col=NA)
abline(v=0.4, col="blue")

enter image description here

Upvotes: 13

Related Questions