For all par(xpd) settings, plot is clipped (Was: [R] Place a graphic into an R-plot)

Roger Bivand Roger.Bivand at nhh.no
Sat Sep 20 19:30:07 CEST 2003


On Sat, 20 Sep 2003, Dirk Eddelbuettel wrote:

> 
> Using Roger's rather useful addlogo() function (still included below), I am
> unable to plot a pixmap anywhere but in the core plot region. 
> 
> In other words, when using the following code, and placing one coordinate
> inside and one coordinate outside the plot region,
> 
> > library(pixmap)
> > logo<-read.pnm(system.file("pictures/logo.ppm", package="pixmap"))
> > source("/tmp/addlogo.R")
> > par(xpd=NA); plot(1:10); addlogo(x=c(2,4),y=c(9,11), pixmap=logo)
> > par(xpd=FALSE); plot(1:10); addlogo(x=c(2,4),y=c(9,11), pixmap=logo)
> > par(xpd=TRUE); plot(1:10); addlogo(x=c(2,4),y=c(9,11), pixmap=logo)
> 
> all attempts result in a partial clipped pixmap (given the 1:10,1:10 plot
> region). Setting xpd inside addlogo()'s plot() made no difference. This was
> using this morning's alpha release of 1.8.0, and on Linux -- but it behaved
> the same on win2k with an older version of R.
> 
> Am I misunderstanding something (which is most likely, given the still
> mysterious ways of ?par), or is this a bug in pixmap's plot method?

No, it seems to be a design choice in do_image() in plot3d.c, near line 
1924:

    /* override par("xpd") and force clipping to plot region */

which is a fine example of well commented code!

Roger

> 
> Thanks, Dirk
> 
> 
> On Thu, Sep 18, 2003 at 03:24:32PM +0200, Roger Bivand wrote:
> > Gordon:
> > 
> > This is a copy of an off-list reply from May 2003, which may give some 
> > assistance - not quite the same, because here the image was inserted into 
> > an existing plot. It is based on using the pixmap package to import a 
> > ppm or pnm file, then rescaling to fit the designated space.
> > 
> > Roger Bivand
> > 
> > ---------- Forwarded message ----------
> > Date: Mon, 26 May 2003 11:26:21 +0200 (CEST)
> > From: Roger Bivand <Roger.Bivand at nhh.no>
> > To: meinhardploner at gmx.net
> > Cc: 
> > Subject: Re: [R] overlapping a plot with an external image
> > 
> > > On Wednesday, May 21, 2003, at 04:38  PM, Prof Brian Ripley wrote:
> > 
> > >> On Wed, 21 May 2003, Meinhard Ploner wrote:
> > >
> > >>> It's possible to overlap an external image (jpg or pdf)
> > >>> with a plot generated with R?
> > >>
> > >>> Specifying the image as the background
> > >>> of the plot might not be possible...
> > >
> > >> Although this has been discussed, R graphics devices cannot as yet plot
> > >> bitmap images.  So all one can do is to plot a set of rectangles: for
> > >> that the pixmap package might be helpful.
> > >
> > >> Although we might add the ability to plot a bitmap image, note that it
> > >> is not straightforward, as R screen graphics devices can be dynamically
> > >> resized.  What should be done with a plotted image then?  Interpolate
> > >> on the fly?
> > 
> > > The plotted image should be a logo of the project / department and I
> > > like to add it on every plot  - for esthetical and descriptive reasons 
> > 
> > Here is a very rough addlogo() using pixmap:
> > 
> > "addlogo" <- function(x, y, pixmap) {
> >     if (is.list(x)) {
> >         y <- x$y
> >         x <- x$x
> >     }
> >     else if (missing(y)) 
> >         stop("missing y")
> >     if (!is.numeric(x) || !is.numeric(y)) 
> >         stop("non-numeric coordinates")
> >     if ((nx <- length(x)) <= 1 || nx != length(y) || nx > 2) 
> >         stop("invalid coordinate lengths")
> >     pixmap at bbox[1] <- x[1]
> >     pixmap at bbox[2] <- y[1]
> >     pixmap at bbox[3] <- x[2]
> >     pixmap at bbox[4] <- y[2]
> >     pixmap at cellres[1] <- (pixmap at bbox[3] - pixmap at bbox[1]) / pixmap at size[2]
> >     pixmap at cellres[2] <- (pixmap at bbox[4] - pixmap at bbox[2]) / pixmap at size[1]
> >     plot(pixmap, add=TRUE)
> >     invisible(pixmap)
> > }
> > 
> > which will work with locator() too. To maintain aspect, it shouldn't alter 
> > the relative cell resolutions, and should just use the new x or y, bur 
> > this is the general case. The handling of the location of the logo is 
> > copied & pasted from legend().
> > 
> > Roger
> > 
> > -- 
> > Roger Bivand
> > Economic Geography Section, Department of Economics, Norwegian School of
> > Economics and Business Administration, Breiviksveien 40, N-5045 Bergen,
> > Norway. voice: +47 55 95 93 55; fax +47 55 95 93 93
> > e-mail: Roger.Bivand at nhh.no
> > 
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> > 
> 
> 

-- 
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Breiviksveien 40, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 93 93
e-mail: Roger.Bivand at nhh.no




More information about the R-help mailing list