patrick
patrick

Reputation: 4852

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.

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!

Upvotes: 5

Views: 3113

Answers (1)

d.b
d.b

Reputation: 32548

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)

Upvotes: 5

Related Questions