From Greg.Snow at imail.org  Mon Mar  3 21:54:25 2008
From: Greg.Snow at imail.org (Greg Snow)
Date: Mon, 3 Mar 2008 13:54:25 -0700
Subject: [Rd] Clipping using par(plt=..., xpd=FALSE) inconsistencies
In-Reply-To: <alpine.LFD.1.00.0802252103290.18007@gannet.stats.ox.ac.uk>
References: <07E228A5BE53C24CAD490193A7381BBBEE1114@LP-EXCHVS07.CO.IHC.COM>
	<Pine.LNX.4.64.0802222050040.11702@auk.stats>
	<alpine.LFD.1.00.0802252103290.18007@gannet.stats.ox.ac.uk>
Message-ID: <07E228A5BE53C24CAD490193A7381BBBF2ACFD@LP-EXCHVS07.CO.IHC.COM>

Thanks,

Sorry I waited so long to get back, I wanted to test it out and waited
for the update to the windows port that includes this.

This is certainly a better approach.  The hist example works great, but
the behaivior of points still has me a bit confused.

> # trying to get a plot with top half blue and bottom half red
> x <- 1:25
> y <- rnorm(25,(x-10)/5)
> plot(x,y, type='b', col='red')
> usr <- par('usr')
> clip( usr[1], usr[2], 0, usr[4] )
> points(x,y,type='b', col='blue')

This does not work, everything is blue. 

But if we do:

> clip( usr[1], usr[2], usr[3], 0 )
> points(x,y,type='b', col='red')

Then only the bottom half gets colored red.  Why does this work with the
second call to clip and points, but not the first?

 From the documentation it says that lines (and I assume points) only
resets the clipping region if  par("xpd") has changed, since xpd has not
been changed I would have expected either both pairs of clip and points
to work, or both not work.

Also when it says that lines and texts "reset it" does this mean that
the clipping region set by clip is only honored when "reset"? or that
these functions reset the clipping region to match the plot region?

(my tests were done using the latest windows port and a default graphics
window)

Thanks,

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
(801) 408-8111
 
 

> -----Original Message-----
> From: Prof Brian Ripley [mailto:ripley at stats.ox.ac.uk] 
> Sent: Monday, February 25, 2008 2:06 PM
> To: Greg Snow
> Cc: r-devel at r-project.org
> Subject: Re: [Rd] Clipping using par(plt=..., xpd=FALSE) 
> inconsistencies
> 
> The following works in R-devel
> 
>       x <- rnorm(1000)
>       hist(x, xlim=c(-4,4))
>       usr <- par("usr")
>       clip(usr[1], -2, usr[3], usr[4])
>       hist(x, col = 'red', add = TRUE)
>       clip(2, usr[2], usr[3], usr[4])
>       hist(x, col = 'blue', add = TRUE)
>       do.call("clip", as.list(usr))  # reset to plot region
> 
> I think that is a general enough mechanism -- see its help 
> page for how long one can expect a clipping region to persist.
> 
> On Fri, 22 Feb 2008, Prof Brian Ripley wrote:
> 
> > I think you misunderstand what par("plt") is supposed to do.  The 
> > documentation says
> >
> >     'plt' A vector of the form 'c(x1, x2, y1, y2)' giving the
> >          coordinates of the plot region as fractions of the current
> >          figure region.
> >
> > You haven't subsequently made a new plot, so why do you expect the 
> > clipping region to be changed to that you indicated for the 
> next plot?
> >
> > I'd say the bug was that box() changed it, and that happens 
> because it 
> > internally sets xpd and so resetting xpd sets the clipping 
> region next 
> > time it is used.  Because of
> >
> > void GClip(pGEDevDesc dd)
> > {
> >    if (gpptr(dd)->xpd != gpptr(dd)->oldxpd) {
> > 	double x1, y1, x2, y2;
> > 	setClipRect(&x1, &y1, &x2, &y2, DEVICE, dd);
> > 	GESetClip(x1, y1, x2, y2, dd);
> > 	gpptr(dd)->oldxpd = gpptr(dd)->xpd;
> >    }
> > }
> >
> > Maybe we should have user-level code to set the clipping region?
> >
> >
> > On Fri, 22 Feb 2008, Greg Snow wrote:
> >
> >> Here is a demonstration of behaviour that is probably an 
> optimization 
> >> by someone far smarter than me that did not anticipate 
> anyone wanting 
> >> to do this, but for my purposes it looks more like a bug 
> than a feature.
> >> 
> >> I have tested this with R2.6.2 on Windows, no additional packages 
> >> loaded (beyond the default), I have tested using the 
> default graphics 
> >> object, pdf, jpeg, and cairoDevice (ok I loaded a package 
> for that) 
> >> and all show the same behavior.
> >> 
> >> Run the following set of commands:
> >> 
> >> x <- rnorm(1000)
> >> hist(x, xlim=c(-4,4))
> >> 
> >> tmp <- par('plt')
> >> 
> >> box(col='#00000000')
> >> tmp2 <- tmp
> >> tmp2[2] <- tmp2[1] + 0.3
> >> par(xpd = FALSE, plt=tmp2)
> >> hist(x, col='red', add=TRUE)
> >> 
> >> box(col='#00000000')
> >> tmp3 <- tmp
> >> tmp3[1] <- tmp3[2] - 0.3
> >> par(xpd=FALSE, plt=tmp3)
> >> hist(x, col='blue', add=TRUE)
> >> par(plt=tmp)
> >> 
> >> 
> >> This gives me the plot that I want and expect (a histogram 
> with the 
> >> left section red, the middle white/background, and the right blue).
> >> 
> >> Now comment out or delete the 2 box commands and rerun 
> everything.  
> >> The clipping does not happen this time and the final 
> result is a full 
> >> blue histogram.
> >> 
> >> Is this a bug? Feature? Something else?
> >> Does anyone have a better work around than drawing 
> transparent boxes?
> >> 
> >> Thanks,
> >> 
> >> 
> >> 
> >> 
> >
> > -- 
> > Brian D. Ripley,                  ripley at stats.ox.ac.uk
> > Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
> > University of Oxford,             Tel:  +44 1865 272861 (self)
> > 1 South Parks Road,                     +44 1865 272866 (PA)
> > Oxford OX1 3TG, UK                Fax:  +44 1865 272595
> >
> 
> -- 
> Brian D. Ripley,                  ripley at stats.ox.ac.uk
> Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
> University of Oxford,             Tel:  +44 1865 272861 (self)
> 1 South Parks Road,                     +44 1865 272866 (PA)
> Oxford OX1 3TG, UK                Fax:  +44 1865 272595
> 



[Rd] Clipping using par(plt=..., xpd=FALSE) inconsistencies
Greg Snow