[R] rasterImage and coordinate conversion
baptiste auguie
baptiste.auguie at googlemail.com
Thu Nov 18 22:59:57 CET 2010
Hi,
For curiosity's sake, below is another version with ggplot2 and Grid graphics,
library(pixmap)
logo <- read.pnm(system.file("pictures/logo.ppm", package="pixmap")[1])
library(ggExtra) # r-forge, requires gridExtra
qplot(rnorm(100),rnorm(100)) +
annotate("pixmap", x=-Inf, y=-Inf, picture=logo, vjust=0, hjust=0, raster=T)
baptiste
On 18 November 2010 20:47, Paul Murrell <paul at stat.auckland.ac.nz> wrote:
> 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/
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
More information about the R-help
mailing list