Creates an evenly spaced, pretty sequence of numbers for a range of a vector.

pretty_range(x, n = NULL, length = NULL)

Arguments

x

A numeric vector.

n

Numeric value, indicating the size of how many values are used to create a pretty sequence. If x has a large value range (> 100), n could be something between 1 to 5. If x has a rather small amount of unique values, n could be something between 10 to 20. If n = NULL, pretty_range() automatically tries to find a pretty sequence.

length

Integer value, as alternative to n, defines the number of intervals to be returned.

Value

A numeric vector with a range corresponding to the minimum and maximum values of x. If x is missing, a function, pre-programmed with n and length is returned. See examples.

Examples

library(sjmisc) data(efc) x <- std(efc$c12hour) x
#> [1] -0.519615996 2.078551270 0.543270613 2.472212977 2.472212977 #> [6] -0.519615996 2.334431379 1.330594027 -0.283418972 -0.047221948 #> [11] 1.133763173 -0.342468228 -0.342468228 -0.362151313 0.267707418 #> [16] -0.440883654 -0.342468228 1.645523392 2.472212977 1.488058709 #> [21] 2.117917441 0.149608906 -0.480249825 2.472212977 -0.539299081 #> [26] 2.472212977 -0.598348337 -0.696763764 -0.145637374 2.472212977 #> [31] 2.117917441 2.472212977 2.472212977 1.507741795 2.472212977 #> [36] 2.472212977 2.472212977 -0.283418972 2.472212977 -0.244052801 #> [41] -0.558982167 2.472212977 2.472212977 0.149608906 2.472212977 #> [46] -0.362151313 2.472212977 -0.007855777 2.196649782 0.346439759 #> [51] 2.472212977 2.472212977 -0.362151313 2.472212977 2.117917441 #> [56] 2.472212977 2.472212977 2.472212977 2.472212977 2.472212977 #> [61] 1.684889563 2.472212977 -0.618031423 0.149608906 0.740101466 #> [66] -0.539299081 -0.696763764 -0.421200569 2.472212977 -0.362151313 #> [71] -0.716446849 -0.244052801 2.472212977 -0.007855777 -0.244052801 #> [76] 0.838516893 -0.145637374 0.543270613 -0.657397593 2.472212977 #> [81] 0.681052210 -0.362151313 0.956615405 -0.716446849 -0.401517484 #> [86] 2.472212977 2.472212977 2.472212977 2.472212977 0.149608906 #> [91] -0.047221948 -0.657397593 -0.342468228 -0.558982167 0.129925821 #> [96] -0.736129935 -0.696763764 -0.421200569 -0.362151313 -0.696763764 #> [101] 2.472212977 2.472212977 -0.755813020 2.472212977 2.472212977 #> [106] -0.539299081 2.472212977 2.472212977 -0.440883654 0.326756674 #> [111] -0.362151313 -0.696763764 1.133763173 2.472212977 2.472212977 #> [116] -0.283418972 -0.440883654 0.149608906 0.917249234 2.472212977 #> [121] -0.598348337 -0.362151313 0.956615405 2.472212977 2.472212977 #> [126] 2.472212977 -0.618031423 -0.145637374 -0.637714508 1.625840307 #> [131] 1.921086587 -0.283418972 0.818833808 -0.539299081 -0.539299081 #> [136] -0.558982167 -0.362151313 -0.755813020 1.921086587 2.472212977 #> [141] -0.716446849 -0.637714508 -0.440883654 -0.539299081 0.444855186 #> [146] -0.145637374 1.232178600 0.149608906 2.472212977 0.681052210 #> [151] 2.472212977 -0.007855777 -0.755813020 2.472212977 2.472212977 #> [156] -0.007855777 -0.716446849 2.472212977 0.543270613 -0.539299081 #> [161] -0.244052801 -0.657397593 -0.519615996 -0.145637374 -0.283418972 #> [166] 2.472212977 -0.657397593 2.472212977 -0.539299081 -0.716446849 #> [171] -0.558982167 -0.145637374 -0.696763764 -0.598348337 0.149608906 #> [176] -0.716446849 -0.401517484 -0.145637374 -0.716446849 2.472212977 #> [181] -0.283418972 -0.637714508 0.956615405 -0.677080679 2.472212977 #> [186] -0.362151313 2.472212977 2.472212977 -0.558982167 -0.283418972 #> [191] -0.519615996 2.472212977 -0.716446849 2.472212977 -0.283418972 #> [196] 2.314748294 -0.066905033 -0.539299081 -0.637714508 -0.637714508 #> [201] -0.499932911 2.472212977 0.543270613 0.149608906 -0.598348337 #> [206] -0.637714508 2.472212977 2.472212977 2.472212977 -0.283418972 #> [211] -0.598348337 -0.677080679 -0.421200569 -0.362151313 -0.539299081 #> [216] -0.677080679 -0.519615996 0.267707418 -0.007855777 -0.755813020 #> [221] -0.342468228 -0.145637374 -0.244052801 -0.716446849 1.330594027 #> [226] -0.244052801 -0.362151313 -0.558982167 -0.755813020 -0.440883654 #> [231] -0.637714508 -0.696763764 2.334431379 -0.283418972 -0.558982167 #> [236] -0.755813020 0.149608906 -0.637714508 -0.480249825 -0.539299081 #> [241] -0.716446849 -0.499932911 -0.558982167 -0.736129935 -0.637714508 #> [246] -0.244052801 -0.637714508 -0.637714508 0.818833808 -0.637714508 #> [251] -0.716446849 -0.539299081 -0.047221948 -0.421200569 -0.618031423 #> [256] -0.677080679 -0.736129935 -0.598348337 -0.637714508 -0.440883654 #> [261] -0.598348337 -0.362151313 -0.303102057 -0.558982167 -0.480249825 #> [266] 2.472212977 -0.558982167 -0.637714508 -0.716446849 -0.421200569 #> [271] -0.519615996 -0.539299081 -0.440883654 -0.637714508 -0.401517484 #> [276] -0.145637374 -0.244052801 -0.519615996 -0.362151313 2.472212977 #> [281] 2.472212977 -0.047221948 -0.047221948 0.149608906 -0.440883654 #> [286] -0.440883654 -0.716446849 -0.736129935 0.110242735 2.472212977 #> [291] -0.716446849 -0.677080679 0.011827308 0.936932320 1.133763173 #> [296] -0.677080679 -0.539299081 -0.736129935 -0.677080679 -0.637714508 #> [301] -0.244052801 2.472212977 -0.440883654 -0.244052801 -0.244052801 #> [306] -0.598348337 -0.244052801 -0.244052801 -0.558982167 -0.539299081 #> [311] 0.149608906 -0.440883654 -0.342468228 -0.047221948 -0.657397593 #> [316] -0.480249825 -0.342468228 1.133763173 -0.716446849 -0.342468228 #> [321] -0.539299081 -0.440883654 -0.637714508 -0.283418972 -0.637714508 #> [326] -0.244052801 -0.716446849 -0.047221948 -0.696763764 -0.677080679 #> [331] -0.539299081 -0.755813020 -0.755813020 2.472212977 -0.696763764 #> [336] -0.401517484 -0.244052801 -0.539299081 -0.244052801 -0.440883654 #> [341] -0.342468228 -0.440883654 -0.440883654 -0.342468228 -0.558982167 #> [346] -0.244052801 -0.440883654 -0.539299081 -0.716446849 -0.736129935 #> [351] 0.149608906 -0.677080679 -0.440883654 -0.558982167 -0.480249825 #> [356] -0.519615996 -0.440883654 -0.539299081 -0.342468228 -0.480249825 #> [361] -0.401517484 -0.480249825 0.110242735 0.346439759 -0.637714508 #> [366] -0.696763764 -0.736129935 -0.145637374 -0.283418972 -0.736129935 #> [371] -0.342468228 -0.047221948 0.129925821 -0.755813020 0.543270613 #> [376] -0.716446849 -0.696763764 -0.047221948 0.149608906 -0.244052801 #> [381] 0.543270613 -0.558982167 0.543270613 -0.696763764 -0.716446849 #> [386] -0.558982167 -0.637714508 -0.342468228 -0.736129935 -0.618031423 #> [391] 1.527424880 -0.480249825 -0.716446849 -0.440883654 -0.539299081 #> [396] -0.145637374 -0.539299081 -0.539299081 1.527424880 -0.283418972 #> [401] -0.637714508 1.527424880 1.133763173 -0.480249825 -0.677080679 #> [406] -0.047221948 1.921086587 -0.716446849 -0.618031423 -0.539299081 #> [411] 0.740101466 -0.736129935 -0.716446849 -0.047221948 -0.736129935 #> [416] -0.696763764 -0.755813020 -0.362151313 -0.283418972 -0.342468228 #> [421] 1.921086587 -0.558982167 -0.047221948 -0.736129935 -0.440883654 #> [426] 2.472212977 -0.047221948 0.740101466 1.527424880 2.314748294 #> [431] -0.696763764 -0.696763764 -0.755813020 -0.716446849 -0.558982167 #> [436] -0.696763764 -0.283418972 -0.342468228 -0.283418972 -0.696763764 #> [441] -0.421200569 -0.244052801 -0.342468228 0.444855186 -0.480249825 #> [446] -0.047221948 -0.440883654 0.740101466 -0.696763764 0.149608906 #> [451] -0.244052801 -0.637714508 -0.677080679 -0.716446849 -0.716446849 #> [456] -0.007855777 -0.637714508 -0.244052801 -0.539299081 -0.637714508 #> [461] -0.637714508 1.724255734 2.314748294 2.472212977 -0.362151313 #> [466] -0.362151313 -0.244052801 -0.440883654 -0.637714508 -0.696763764 #> [471] 1.527424880 -0.716446849 -0.716446849 -0.637714508 -0.440883654 #> [476] -0.736129935 0.346439759 -0.440883654 -0.716446849 -0.637714508 #> [481] 0.818833808 -0.677080679 2.472212977 -0.047221948 -0.047221948 #> [486] -0.440883654 0.267707418 -0.716446849 -0.696763764 -0.598348337 #> [491] -0.637714508 -0.558982167 -0.283418972 -0.598348337 -0.283418972 #> [496] -0.618031423 -0.519615996 -0.598348337 -0.401517484 -0.499932911 #> [501] -0.519615996 0.149608906 -0.716446849 -0.736129935 0.051193479 #> [506] -0.519615996 -0.539299081 -0.440883654 -0.440883654 -0.677080679 #> [511] -0.677080679 -0.539299081 -0.637714508 -0.539299081 -0.303102057 #> [516] -0.558982167 -0.283418972 2.472212977 2.472212977 -0.421200569 #> [521] -0.598348337 0.385805930 -0.440883654 -0.696763764 -0.637714508 #> [526] -0.440883654 -0.677080679 -0.657397593 -0.598348337 -0.637714508 #> [531] -0.145637374 -0.736129935 -0.480249825 -0.539299081 -0.716446849 #> [536] -0.677080679 -0.598348337 -0.342468228 -0.637714508 -0.047221948 #> [541] -0.440883654 -0.716446849 2.472212977 -0.755813020 2.472212977 #> [546] -0.362151313 -0.342468228 -0.047221948 -0.480249825 -0.440883654 #> [551] -0.047221948 -0.716446849 -0.736129935 -0.244052801 -0.558982167 #> [556] -0.677080679 -0.283418972 0.051193479 -0.047221948 -0.244052801 #> [561] -0.283418972 0.149608906 0.818833808 -0.598348337 -0.047221948 #> [566] -0.637714508 -0.539299081 -0.047221948 -0.362151313 2.472212977 #> [571] 2.314748294 2.354114465 1.133763173 1.330594027 1.921086587 #> [576] -0.342468228 -0.539299081 -0.145637374 -0.244052801 -0.047221948 #> [581] -0.637714508 -0.342468228 -0.598348337 -0.145637374 -0.637714508 #> [586] -0.244052801 -0.637714508 1.133763173 -0.598348337 0.267707418 #> [591] -0.047221948 -0.244052801 0.346439759 1.133763173 -0.440883654 #> [596] -0.145637374 2.472212977 -0.401517484 0.346439759 -0.362151313 #> [601] -0.440883654 0.543270613 -0.342468228 -0.716446849 -0.480249825 #> [606] -0.637714508 -0.539299081 -0.440883654 -0.440883654 -0.637714508 #> [611] -0.401517484 -0.539299081 0.110242735 0.740101466 0.681052210 #> [616] -0.716446849 -0.716446849 -0.677080679 -0.440883654 -0.440883654 #> [621] -0.677080679 -0.716446849 -0.677080679 -0.539299081 -0.637714508 #> [626] -0.637714508 -0.539299081 -0.677080679 -0.716446849 0.267707418 #> [631] -0.716446849 -0.677080679 -0.283418972 -0.440883654 0.543270613 #> [636] -0.677080679 -0.696763764 -0.637714508 -0.637714508 -0.637714508 #> [641] 2.472212977 -0.047221948 -0.047221948 -0.244052801 -0.244052801 #> [646] -0.283418972 -0.558982167 -0.047221948 -0.637714508 -0.755813020 #> [651] -0.558982167 -0.007855777 -0.007855777 0.444855186 -0.736129935 #> [656] -0.637714508 0.051193479 0.248024333 0.149608906 -0.145637374 #> [661] -0.047221948 -0.539299081 -0.677080679 -0.342468228 -0.736129935 #> [666] -0.736129935 -0.755813020 -0.558982167 -0.440883654 -0.637714508 #> [671] 0.149608906 -0.519615996 -0.401517484 1.133763173 1.133763173 #> [676] -0.244052801 1.527424880 1.133763173 -0.637714508 1.527424880 #> [681] -0.716446849 -0.716446849 -0.736129935 -0.637714508 -0.657397593 #> [686] -0.657397593 -0.598348337 -0.440883654 -0.637714508 2.314748294 #> [691] 1.133763173 -0.440883654 0.267707418 -0.696763764 -0.145637374 #> [696] 1.527424880 0.346439759 -0.637714508 -0.558982167 -0.421200569 #> [701] -0.637714508 -0.696763764 -0.480249825 -0.755813020 -0.677080679 #> [706] -0.637714508 -0.716446849 -0.637714508 -0.244052801 -0.637714508 #> [711] -0.657397593 -0.125954289 -0.539299081 -0.145637374 -0.421200569 #> [716] 2.472212977 2.472212977 -0.637714508 -0.716446849 -0.440883654 #> [721] -0.716446849 -0.677080679 -0.539299081 0.818833808 0.051193479 #> [726] -0.047221948 0.936932320 -0.145637374 0.543270613 -0.736129935 #> [731] -0.440883654 -0.145637374 -0.677080679 -0.244052801 -0.755813020 #> [736] 0.818833808 -0.637714508 -0.342468228 0.346439759 -0.716446849 #> [741] -0.322785142 -0.598348337 -0.342468228 -0.539299081 0.149608906 #> [746] -0.480249825 -0.598348337 -0.401517484 -0.421200569 0.543270613 #> [751] 2.472212977 -0.736129935 -0.244052801 0.543270613 2.472212977 #> [756] -0.657397593 -0.716446849 -0.755813020 -0.440883654 -0.677080679 #> [761] -0.558982167 -0.657397593 -0.716446849 -0.558982167 -0.696763764 #> [766] -0.716446849 -0.440883654 -0.440883654 0.110242735 -0.637714508 #> [771] -0.736129935 -0.716446849 -0.677080679 -0.677080679 -0.677080679 #> [776] -0.677080679 -0.755813020 -0.755813020 -0.677080679 -0.342468228 #> [781] -0.440883654 -0.716446849 -0.421200569 -0.283418972 0.543270613 #> [786] -0.677080679 -0.421200569 -0.283418972 -0.558982167 -0.755813020 #> [791] -0.736129935 -0.716446849 -0.696763764 1.921086587 -0.755813020 #> [796] -0.677080679 -0.696763764 -0.677080679 -0.696763764 -0.244052801 #> [801] -0.047221948 0.149608906 -0.145637374 -0.007855777 -0.047221948 #> [806] 2.472212977 2.117917441 2.472212977 2.472212977 2.472212977 #> [811] -0.558982167 -0.716446849 -0.677080679 -0.401517484 -0.637714508 #> [816] -0.716446849 -0.244052801 -0.677080679 -0.637714508 -0.637714508 #> [821] -0.598348337 -0.440883654 -0.539299081 -0.440883654 -0.539299081 #> [826] -0.440883654 -0.342468228 -0.696763764 -0.755813020 -0.440883654 #> [831] -0.637714508 0.149608906 -0.440883654 -0.342468228 -0.736129935 #> [836] -0.696763764 -0.736129935 -0.598348337 -0.145637374 -0.145637374 #> [841] -0.244052801 -0.047221948 -0.539299081 -0.637714508 -0.677080679 #> [846] -0.736129935 -0.558982167 -0.558982167 -0.716446849 -0.558982167 #> [851] -0.696763764 -0.440883654 -0.716446849 -0.637714508 -0.244052801 #> [856] -0.558982167 -0.637714508 -0.637714508 -0.283418972 -0.677080679 #> [861] -0.716446849 -0.440883654 -0.736129935 -0.696763764 -0.440883654 #> [866] -0.480249825 -0.598348337 -0.499932911 -0.558982167 -0.736129935 #> [871] -0.716446849 -0.755813020 -0.755813020 -0.716446849 -0.637714508 #> [876] -0.716446849 -0.558982167 -0.244052801 -0.145637374 -0.637714508 #> [881] 1.133763173 -0.677080679 -0.637714508 -0.440883654 -0.047221948 #> [886] -0.047221948 -0.047221948 -0.716446849 -0.244052801 -0.283418972 #> [891] -0.440883654 -0.342468228 -0.244052801 0.149608906 -0.440883654 #> [896] -0.539299081 1.330594027 -0.283418972 0.838516893 2.314748294 #> [901] -0.637714508 -0.677080679 NA NA NA #> [906] NA NA NA #> attr(,"label") #> [1] "average number of hours of care per week"
# pretty range for vectors with decimal points pretty_range(x)
#> [1] -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 2.5
# pretty range for large range, increasing by 50 pretty_range(1:1000)
#> [1] 0 50 100 150 200 250 300 350 400 450 500 550 600 650 700 #> [16] 750 800 850 900 950 1000
# increasing by 20 pretty_range(1:1000, n = 7)
#> [1] 0 20 40 60 80 100 120 140 160 180 200 220 240 260 280 #> [16] 300 320 340 360 380 400 420 440 460 480 500 520 540 560 580 #> [31] 600 620 640 660 680 700 720 740 760 780 800 820 840 860 880 #> [46] 900 920 940 960 980 1000
# return 10 intervals pretty_range(1:1000, length = 10)
#> [1] 0 100 200 300 400 500 600 700 800 900 1000
# same result pretty_range(1:1000, n = 2.5)
#> [1] 0 100 200 300 400 500 600 700 800 900 1000
# function factory range_n_5 <- pretty_range(n = 5) range_n_5(1:1000)
#> [1] 0 50 100 150 200 250 300 350 400 450 500 550 600 650 700 #> [16] 750 800 850 900 950 1000