-
Notifications
You must be signed in to change notification settings - Fork 0
/
fnc.R
92 lines (72 loc) · 3 KB
/
fnc.R
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
#' simple find the peaks in an univariate serie
#' replace all non peaks by 0
#'
#' @param threshold
#' @param nsamples.ignore number of sample after a peaks to ignore (avoid false detection)
#' @param norm TRUE to normalize peaks to 1, FALSE keep the value of the peaks
findPeaks <- function(x, threshold=50, nsamples.ignore=1000, norm=FALSE){
N <- length(x)
it <- 1
# Clearly not an R friendly piece of code, but didn't find a clever way
while(TRUE){
if((x[it] > threshold)==TRUE){
if (norm){
x[it] <- 1
}
x[(it+1):(it+nsamples.ignore)] <- 0
it <- it + nsamples.ignore
}else{
x[it] <- 0
it <- it + 1
}
if (it > N) return(x)
}
}
calc.bpm <- function(peaks, fs){
diff.peaks <- diff(which(peaks==1))
bpm <- 1/diff.peaks*fs*60
}
#' ggplot2 theme from minimaxir.com
#'
#' @param scale_text use to scale the size of the text
#' @param font Well, the font you want
#' @examples
#' ggplot(data=data.frame(x=1:100, y=rnorm(100)),aes(x=x,y=y))+
#' geom_point()+labs(title="rnd", x="x",y="y")+theme_mini(scale_text=1.5)
theme_mini <- function(scale_text=1, font="Impact") {
# best font = "Impact", Times New Roman, Helvetica, Garamond, Futura, Lowetica
# NEW BEST = "Arial Black", "Comfortaa", "Impact"
# Generate the colors for the chart procedurally with RColorBrewer
# Credit: Max Woolf http://minimaxir.com
library(RColorBrewer)
library(grid)
palette <- brewer.pal("Greys", n=9)
color.background = palette[1]
color.grid.major = palette[3]
color.axis.text = palette[6]
color.axis.title = palette[7]
color.title = palette[9]
# Begin construction of chart
theme_bw(base_size=9) +
# Set the entire chart region to a light gray color
theme(text=element_text(family=font))+
theme(panel.background=element_rect(fill=color.background, color=color.background)) +
theme(plot.background=element_rect(fill=color.background, color=color.background)) +
theme(panel.border=element_rect(color=color.background)) +
# Format the grid
theme(panel.grid.major=element_line(color=color.grid.major,size=.25)) +
theme(panel.grid.minor=element_blank()) +
theme(axis.ticks=element_blank()) +
# Format the legend, but hide by default
#theme(legend.position="none") +
theme(legend.background = element_rect(fill=color.background)) +
theme(legend.text = element_text(size=7*scale_text,color=color.axis.title)) +
# Set title and axis labels, and format these and tick marks
theme(plot.title=element_text(color=color.title, size=10*scale_text, vjust=1.25)) +
theme(axis.text.x=element_text(size=7*scale_text,color=color.axis.text)) +
theme(axis.text.y=element_text(size=7*scale_text,color=color.axis.text)) +
theme(axis.title.x=element_text(size=8*scale_text,color=color.axis.title, vjust=0)) +
theme(axis.title.y=element_text(size=8*scale_text,color=color.axis.title, vjust=1.25)) +
# Plot margins
theme(plot.margin = unit(c(0.35, 0.2, 0.3, 0.35), "cm"))
}