rplotlm

Plot function issue - adding a line


My goal is to plot a line (red) using the plot function. This is what I have tried so far.

data("Nile")
plot(Nile)
fm0 <- lm(Nile ~ 1) ; summary(fm0)
y1 <- c(rep(1097.75, 20), rep(848.9722, 80))
 # Plot 
lines(fitted(fm0), col = "blue", lty = "dotted", lwd = 1)
lines(y1, time(Nile), col = "red",  lwd = 2)

The line in red color is not showing. Not sure where I am going wrong.

Expecting a plot like this

enter image description here


Solution

  • Ah, so you're looking for what @MrFlick suggested above.

    data("Nile")
    plot(Nile)
    fm0 <- mean(Nile)
    y1 <- c(rep(1097.75, 20), rep(848.9722, 80))
    abline(h = fm0, col = "blue", lty = "dotted", lwd = 1)
    lines(c(time(Nile)), y1, col = "red",  lwd = 2)
    

    which yields: enter image description here