rggplot2plotlineautocorrelation

Add vertical lines connecting point to a horizontal line in a plot in R


I am plotting some values of autocorrelation in a R:

  plot(y=lag[2:N],x=1:(N-1), xlab="lag",ylab="Autocorrelation",ylim=c(-1,1), pch=16,col="red")
  abline(h=0, col="black")
  abline(h=up, col="blue")
  abline(h=low, col="blue")

This is my code and this is what I got in R enter image description here

However, I want something like the image below, where I connect the points with a red line to the horizontal line at 0.

enter image description here

Any idea how to do it?


Solution

  • You can add vertical lines with type = "h", and then add the points separately

    plot(y=lag[2:N],x=1:(N-1), xlab="lag",ylab="Autocorrelation",ylim=c(-1,1), col="black", type = "h") 
    points(y=lag[2:N],x=1:(N-1), xlab="lag", ylim=c(-1,1), pch=16,col="red", type = "p")