Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# \\\
# Copyright 2021-2022 Louis Héraut*1
#
# *1 INRAE, France
# louis.heraut@inrae.fr
#
# This file is part of ash R toolbox.
#
# ash R toolbox is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
#
# ash R toolbox is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ash R toolbox. If not, see <https://www.gnu.org/licenses/>.
# ///
#
#
# tools.R
### 2.3. Circle ______________________________________________________
# Allow to draw circle in ggplot2 with a radius and a center position
gg_circle = function(r, xc, yc, color="black", fill=NA, ...) {
x = xc + r*cos(seq(0, pi, length.out=100))
ymax = yc + r*sin(seq(0, pi, length.out=100))
ymin = yc + r*sin(seq(0, -pi, length.out=100))
annotate("ribbon", x=x, ymin=ymin, ymax=ymax, color=color,
fill=fill, ...)
}
## 6. OTHER TOOLS ____________________________________________________
### 6.1. Number formatting ___________________________________________
# Returns the power of ten of the scientific expression of a value
get_power = function (value) {
# Do not care about the sign
value = abs(value)
# If the value is greater than one
if (value >= 1) {
# The magnitude is the number of character of integer part
# of the value minus one
power = nchar(as.character(as.integer(value))) - 1
# If value is zero
} else if (value == 0) {
# The power is zero
power = 0
# If the value is less than one
} else {
# Extract the decimal part
dec = gsub('0.', '', as.character(value), fixed=TRUE)
# Number of decimal with zero
ndec = nchar(dec)
# Number of decimal without zero
nnum = nchar(as.character(as.numeric(dec)))
# Compute the power of ten associated
power = -(ndec - nnum + 1)
}
return(power)
}
### 6.2. Pourcentage of variable _____________________________________
# Returns the value corresponding of a certain percentage of a
# data serie
gpct = function (pct, L, min_lim=NULL, shift=FALSE) {
# If no reference for the serie is given
if (is.null(min_lim)) {
# The minimum of the serie is computed
minL = min(L, na.rm=TRUE)
# If a reference is specified
} else {
# The reference is the minimum
minL = min_lim
}
# Gets the max
maxL = max(L, na.rm=TRUE)
# And the span
spanL = maxL - minL
# Computes the value corresponding to the percentage
xL = pct/100 * as.numeric(spanL)
# If the value needs to be shift by its reference
if (shift) {
xL = xL + minL
}
return (xL)
}
### 6.3. Add months __________________________________________________
add_months = function (date, n) {
new_date = seq(date, by = paste (n, "months"), length = 2)[2]
return (new_date)
}