Search code examples
rplotpar

Draw abline outside of plot margins on one side only


I'm working on an R plot that will be separated into two parts by a vertical line I'm creating with abline().I would like that abline to go over the boundaries of my plot on one side only.

I found this helpful post about setting par(xpd=). However, I could not figure out how to get this command make the line go over the plot border on one side only, as outlined in the screenshot below.

  • Is there a way to do this in base R? (Or do I have to rebuild the whole thing in ggplot etc?)

  • I feel that the key might the differences between figure region, device region, etc. but was unable to really determine the difference between those. I looked at this source but at least for my plot those settings seem to amount to the same thing - it would be cool if anyone had some help here as well!

Here's my approach:

plot(100, 100)
par(xpd=TRUE)
abline(v=70, lty=3)

And what I get / want:

My plot

Any help is appreciated!


Solution

  • Use lines. Set limits of y as you want.

    plot(100, 100)
    par(xpd = TRUE)
    lines(x = c(70,70), y = c(45, par('usr')[4]), lty = 2)
    # par('usr') gives the 4 extremes of plot
    # par('usr')[4] gives the extreme on top
    

    Another option is to not mess with xpd and use axis instead. Use tck to define the length of line beyond the plot. tck = -0.25 means the length is one fourth of the plot height towards bottom.

    plot(100, 100)
    par(xpd = FALSE) #Only because we made TRUE above 
    abline(v = 70, lty = 2)
    axis(1, at = 70, labels = NA, tck = -0.25, lty = 2)