screen2data converts between screen (pixel) coordinates of device output and the data coordinates.

data2screen performs the inverse conversion.

Both functions can be parametrized manually but default to obtaining all necessary parameters from the active graphics device. It is most useful when used with bitmap dvices such as the Cairo device (see Cairo package) for the purpose of adding interactivity (e.g., via JavaScript).

screen2data(x, y, width = par("fin")[1] * dpi, height = par("fin")[2] * dpi,
            dpi = 72, plt = par("plt"), usr = par("usr"), flip = FALSE)
data2screen(x, y, width = par("fin")[1] * dpi, height = par("fin")[2] * dpi,
            dpi = 72, plt = par("plt"), usr = par("usr"), flip = FALSE)

Arguments

x

x coordinates of locations to convert or a two-column matrix (if y is missing)

y

y coordinates of locations to convert (if missing x must be a matrix and the second column of x is interpreted as y)

width

width of the figure region (usually the size of the resulting file in pixels)

height

height of the figure region (usually the size of the resulting file in pixels)

dpi

resolution (only used to compute the width and height from figure size if they are not specified

plt

the `plt' parameter

usr

the `usr' parameter

flip

if set to TRUE then y axis in pixels is assumed to be flipped (0 on top)

Value

The result is a two-column matrix with the columns x and y. The special case of one row input is returned as a named vector of length two.

Note

If x and y are vectors they are recycled to match.

Examples

plot(0:1,0:1)
#> Warning: calling par(new=TRUE) with no plot
#> Warning: calling par(new=TRUE) with no plot
#> Error in plot.new(): figure margins too large
## where on the bitmap is the data point 0,0 ?
data2screen(0, 0)
#>     x     y 
#> 59.04 73.44 
## if I click on 200, 100 with flipped coordinates, what coordinates do
## I hit?
screen2data(200, 100, flip=TRUE)
#>        x        y 
#> 3.838780 1.499025 
## go there and back
screen2data(data2screen(c(0, 0.5), c(1, 0.5)))
#>        x   y
#> [1,] 0.0 1.0
#> [2,] 0.5 0.5