Skip to content

Is abline misbehaving?

2 messages · Jim Lemon, Greg Snow

#
Hi experts,
I was graciously offered a function to enhance abline by restricting the 
extent of the line to less than the plotting region. This seems a useful 
idea, and it looked like the easiest way to program it was to set up a 
clipping region with "clip", draw the abline and then restore the 
previous clipping region. Let us call this function ablineclip. After 
quite a bit of testing, I have found that the first call to ablineclip 
ignores the clipping region. It's not that simple. Successive calls to 
ablineclip respect the clipping region, even if it changes. I can 
reproduce the behavior like this:

plot(-3:3,-3:3)
clip(-2,2,-2,2)
abline(v=0)
clip(-2,2,-2,2)
abline(h=0)


The first abline ignores the clip, the second respects it. I have 
programmed around this, with the pathetic kludge of calling "abline" 
with a line outside the plotting area, calling "clip" a second time, and 
then calling "abline" with the line that was requested. While this 
works, my place in programming history will be ineradicably compromised 
if the Programmers' Hall of Fame ever finds out. Any suggestions?

R-2.7.2
FC9 Linux

Jim
#
This is a known issue, the documentation of clip talks about some plotting functions resetting the clipping region and some don't.  abline is apparently one of those that plots first, then resets the clipping region (so the first time it doesn't acknowledge it, but does afterwards).  The function clipplot in the TeachingDemos package uses a similar kludge to what you do below (and I guess that puts my standing in the hall of fame at even higher risk than yours) except that it uses the box function with a transparent box to reset the clipping region, which means that you get strange boxes if your graphics device does not handle transparency.  It was a question like yours that I asked in order to try to eliminate the kludge from clipplot that originally lead to the clip function being added, and it does cover the initial cases that I asked about.

In order to change things to work like we would like (always resetting the clipping region at the appropriate place so that clip always does what we expect) will probably require going through every basic command that could put something into the plot and figure out exactly when to have them reset the clipping region (which may not be a simple decision, doing it too early may break existing code).  The amount of tedious work required for not much return places this fairly low on the priority list, especially when there is a work around (as you have found), even if it feels like a bit of a kludge.

So while this probably does not fix your problem, at least hopefully this helps you understand a bit more of what is happening here, and at least you know that you are not alone in hall of kludge infamy.