I am writing a function to plot data. I would like to specify a nice round number for the y-axis max that is greater than the max of the dataset.
Specif
You will find an upgraded version of Tommy's answer that takes into account several cases:
Below the code :
round.up.nice <- function(x, lower_bound = TRUE, nice_small=c(0,5,10), nice_big=c(1,2,3,4,5,6,7,8,9,10)) {
if (abs(x) > 100) {
nice = nice_big
} else {
nice = nice_small
}
if (lower_bound == TRUE) {
if (x > 0) {
return(10^floor(log10(x)) * nice[[max(which(x >= 10^floor(log10(x)) * nice))[[1]]]])
} else if (x < 0) {
return(- 10^floor(log10(-x)) * nice[[min(which(-x <= 10^floor(log10(-x)) * nice))[[1]]]])
} else {
return(0)
}
} else {
if (x > 0) {
return(10^floor(log10(x)) * nice[[min(which(x <= 10^floor(log10(x)) * nice))[[1]]]])
} else if (x < 0) {
return(- 10^floor(log10(-x)) * nice[[max(which(-x >= 10^floor(log10(-x)) * nice))[[1]]]])
} else {
return(0)
}
}
}