[R] rasterImage and coordinate conversion
Paul Murrell
paul at stat.auckland.ac.nz
Thu Nov 18 20:47:03 CET 2010
Hi
Rajarshi Guha wrote:
> Hi, I have a plot and I would like to overlay a PNG image over it. I'm
> using the rasterImage function to do this, but the problem I'm facing
> is working out the coordinates of the upper right corner of the final
> image in user coordinates.
>
> That is I can place the image so the lower left is located at the
> bottom of the y-axis and the left end of the x-axis. Since my image is
> say 100px x 100px, is there a way for me to convert a 100px length
> into the appropriate value in user coordinates, along the x-axis?
Are you trying to draw the image at its "native" resolution?
R should be able to scale the image using interpolation to whatever size
you want, but if you really want to try for native resolution, this
should get you close ...
# Import a raster image
library(pixmap)
logo <- read.pnm(system.file("pictures/logo.ppm", package="pixmap")[1])
# Image size
logo at size
# A plot
plot(rnorm(100), rnorm(100))
# If you know the screen resolution, use that, otherwise
# the device may be telling R something close to the truth
dpi <- (par("cra")/par("cin"))[1]
usr <- par("usr")
xl <- usr[1]
yb <- usr[3]
xr <- xl + xinch(logo at size[2]/dpi)
yt <- yb + yinch(logo at size[1]/dpi)
# Image at "native" resolution
rasterImage(matrix(rgb(logo at red, logo at green, logo at blue),
nrow=logo at size[1]),
xl, yb, xr, yt)
Paul
> Thanks,
>
>
--
Dr Paul Murrell
Department of Statistics
The University of Auckland
Private Bag 92019
Auckland
New Zealand
64 9 3737599 x85392
paul at stat.auckland.ac.nz
http://www.stat.auckland.ac.nz/~paul/
More information about the R-help
mailing list