Checks and returns selected coordinates from multiple input options, which can be either data (x-y) coordinates or npc (normalized parent coordinates).

Helper function internally used in ggpubr function to guess the type of coordinates specified by the user. For example, in the function stat_cor(), users can specify either the option label.x (data coordinates) or label.x.npc (npc coordinates); those coordinates are passed to get_coord(), which will make some checking and then return a unique coordinates for the label position.

get_coord(
  group = 1L,
  data.ranges = NULL,
  coord = NULL,
  npc = "left",
  step = 0.1,
  margin.npc = 0.05
)

Arguments

group

integer ggplot's group id. Used to shift coordinates to avoid overlaps.

data.ranges

a numeric vector of length 2 containing the data ranges (minimum and the maximum). Should be specified only when coord = NULL and npc is specified. Used to convert npc to data coordinates. Considered only when the argument npc is specified.

coord

data coordinates (i.e., either x or y coordinates).

npc

numeric (in [0-1]) or character vector of coordinates. If character, should be one of c('right', 'left', 'bottom', 'top', 'center', 'centre', 'middle'). Note that, the data.ranges, step and margin.npc, arguments are considered only when npc is specified. The option npc is ignored when the argument coord is specified.

step

numeric value in [0-1]. The step size for shifting coordinates in npc units. Considered as horizontal step for x-axis and vertical step for y-axis. For y-axis, the step value can be negative to reverse the order of groups.

margin.npc

numeric [0-1] The margin added towards the nearest plotting area edge when converting character coordinates into npc.

Value

a numeric vector representing data coordinates.

Examples

# If npc is specified, it is converted into data coordinates
get_coord(data.ranges = c(2, 20), npc = "left")
#> [1] 2.9
get_coord(data.ranges = c(2, 20), npc = 0.1)
#> [1] 3.8

# When coord is specified, no transformation is performed
# because this is assumed to be a data coordinate
get_coord(coord = 5)
#> [1] 5

# For grouped plots
res_top <- get_coord(
  data.ranges = c(4.2, 36.4), group = c(1, 2, 3),
  npc = "top", step = -0.1, margin.npc = 0
)
res_top
#> [1] 36.40 39.62 42.84