This function maps a set of values to a range of integers (from 1 to a positive integer passed as an argument).

scaleWithin(x, n = 100, mn = min(x), mx = max(x))

Arguments

x

a numeric object of type numeric or coercible as one.

n

an integer designing The number of scaled values (between 1 and n).

mn

minimum value (mn and lower values are set to 1).

mx

maximum value (mx and higher values are set to n).

Value

A numeric vector

Details

This function was originally created to ease the creation of custom color scales. In such context, it is common practice to define a minium and a maximum for the set of values to be displayed values as well as a number of color to be used in the color scale (n). This is exactly what scaleWithin() does: minimum and maximum are by default the ones of the set of values x but can also be set using mn and mx in which case values below mn will be set to mn (and thus to 1 in the output) and values above mx will be set to mx (and thus to 1 in the output). Finally, the number of colors to be used used in is given by n.

Examples

x <- stats::rpois(20, 20) scaleWithin(x, 20, 10, 30)
#> [1] 10 13 13 12 9 12 15 14 5 9 13 15 5 9 12 9 5 6 9 13
scaleWithin(matrix(x, 10, 2), mn = 20, mx = 30)
#> [,1] [,2] #> [1,] 1 21 #> [2,] 21 41 #> [3,] 21 1 #> [4,] 11 1 #> [5,] 1 11 #> [6,] 11 1 #> [7,] 41 1 #> [8,] 31 1 #> [9,] 1 1 #> [10,] 1 21