Learn R Programming

plotfunctions (version 1.4)

getRatioCoords: Move a vector n elements forward or backward.

Description

Move a vector n elements forward or backward.

Usage

getRatioCoords(
  ratio,
  width = NULL,
  height = NULL,
  input = c("coords", "prop"),
  ...
)

Arguments

ratio

Numeric, height : width ratio. If ratio > 1, the width is larger than the height, if ration < 1, the height is larger than the width.

width

The desired width in plot coordinates or proportions. If not specified (NULL), the maximal width fitting in the plot region is returned.

height

The desired height in plot coordinates or proportions. If not specified (NULL), the maximal height fitting in the plot region is returned.

input

Unit of input width and height, 'coords' (plot coordinates, default), or 'prop' (proportions of plot region).

...

Optional arguments: xcenter, xleft, or xright, and ycenter, ybottom, or ytop could be specified. If not specified, the width and height are centered around the center of the plot.

Value

A list with 5 elements:

  • width: width of the element in x-axis coordinates;

  • height: height of the element in y-axis coordinates;

  • ratio: provided ratio (for confirmation);

  • x: two-number vector with x-coordinates of left and right sides;

  • y: two-number vector with y-coordinates of bottom and top sides.

See Also

Other Utility functions: findAbsMin(), find_n_neighbors(), firstLetterCap(), getArrowPos(), getDec(), getRange(), get_palette(), group_sort(), inch2coords(), isColor(), list2str(), move_n_point(), orderBoxplot(), se(), sortGroups()

Examples

Run this code
# NOT RUN {
data(img)
emptyPlot(100, c(50, 100), h0=0, v0=0)
# calculate height : width ratio of image:
im.r <- dim(img$image)[1]/dim(img$image)[2]
p <- getRatioCoords(ratio=im.r, width=20)
# inspect p:
p
# No position specified, so centered:
plot_image(img, type='image', add=TRUE,
    xrange=p$x, yrange=p$y)
# ... or we could provide a position:
p <- getRatioCoords(ratio=im.r, width=20,
    xleft=20, ybottom=60)
plot_image(img, type='image', add=TRUE,
    xrange=p$x, yrange=p$y)

# Using proportions of plot region:
p <- getRatioCoords(ratio=im.r, height=.5,
    xleft=0, ytop=1, input='prop')
plot_image(img, type='image', add=TRUE,
    xrange=p$x, yrange=p$y)

# Changing the ratio to square:
p <- getRatioCoords(ratio=1, height=.5,
    xright=1, ybottom=0, input='prop')
plot_image(img, type='image', add=TRUE,
    xrange=p$x, yrange=p$y)
# ... and to a long rectangle:
p <- getRatioCoords(ratio=.5, height=1,
    xright=1, ybottom=0, input='prop')
plot_image(img, type='image', add=TRUE,
    xrange=p$x, yrange=p$y, 
    replace.colors=list('#B.+'='#FF000033'),
    border='red')

# }

Run the code above in your browser using DataLab