[R] Place a graphic into an R-plot

Roger Bivand Roger.Bivand at nhh.no
Thu Sep 18 15:24:32 CEST 2003


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




More information about the R-help mailing list