'generate human-frinedly ticks in power scale
I want to find an algorithm to generate ticks in a power scale in a human-friendly way.
For example, if the power is 1/2, between the range of [0, 100], without considering human friendliness, the ticks may be (0, 1, 4, 9, 25, 36, 49, 64, 81, 100).
However, in order to make a plot in 1/2-power scale labeled with the ticks, it would be better to round the ticks to things in multiple of 1's 2's 5's and 10's for this specific example whenever appropriate.
So the human-friendly version of the numbers may be (0, 1, 5, 10, 25, 35, 50, 65, 80, 100) (if the input parameter of the number of ticks is around 10). This is easy to be done manually for specific examples like this.
How to come up with a general algorithm that will work for any positive power and any non-negative intervals (note the interval boundaries may not integers, they can be arbitrary positive real numbers) so that the algorithmic result would be the same as what would be chosen by a human?
Solution 1:[1]
This is how to round a power sequence to numbers dividable by 5:
power <- 2
a <- sapply(seq(10), function(x) x ** power)
a
#> [1] 1 4 9 16 25 36 49 64 81 100
b <- seq(0, 100, by = 5)
sapply(a, function(a, b) {b[which.min(abs(a-b))]}, b)
#> [1] 0 5 10 15 25 35 50 65 80 100
Created on 2022-04-29 by the reprex package (v2.0.0)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | danlooo |
