Quantcast

rasterImage and coordinate conversion

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

rasterImage and coordinate conversion

Rajarshi Guha-4
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?

Thanks,


--
Rajarshi Guha
NIH Chemical Genomics Center

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: rasterImage and coordinate conversion

Paul Murrell-2
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@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@size[2]/dpi)
yt <- yb + yinch(logo@size[1]/dpi)

# Image at "native" resolution
rasterImage(matrix(rgb(logo@red, logo@green, logo@blue),
                    nrow=logo@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
[hidden email]
http://www.stat.auckland.ac.nz/~paul/

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: rasterImage and coordinate conversion

baptiste auguie-5
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 <[hidden email]> 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@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@size[2]/dpi)
> yt <- yb + yinch(logo@size[1]/dpi)
>
> # Image at "native" resolution
> rasterImage(matrix(rgb(logo@red, logo@green, logo@blue),
>                   nrow=logo@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
> [hidden email]
> http://www.stat.auckland.ac.nz/~paul/
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Loading...