annotate nmr_preprocessing/DrawFunctions.R @ 2:7304ec2c9ab7 draft

Uploaded
author marie-tremblay-metatoul
date Mon, 30 Jul 2018 10:33:03 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
1 require(ggplot2)
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
2 require(gridExtra)
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
3 require(reshape2)
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
4
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
5
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
6 Draw <- function(Signal_data, type.draw = c("signal", "pca"), output = c("default",
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
7 "window", "png", "pdf"), dirpath = ".", filename = "%003d", height = 480,
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
8 width = 640, pdf.onefile = TRUE, ...) {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
9
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
10 # Data initialisation and checks ----------------------------------------------
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
11 type.draw <- match.arg(type.draw)
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
12 output <- match.arg(output)
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
13 fullpath <- paste(file.path(dirpath, filename), output, sep = ".")
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
14 createFile <- TRUE
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
15 createWindow <- FALSE
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
16
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
17 # Drawing --------------------------------------------------------------------
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
18 # output
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
19 switch(output, default = {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
20 createFile <- FALSE
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
21 }, window = {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
22 createWindow <- TRUE
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
23 createFile <- FALSE
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
24 }, png = {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
25 grDevices::png(fullpath, width, height)
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
26 }, pdf = {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
27 grDevices::pdf(fullpath, width = width/72, height = height/72,
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
28 onefile = pdf.onefile)
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
29 }, {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
30 stop("Unknown output type.")
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
31 })
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
32
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
33 # Drawing type (signal/spectrum or PCA)
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
34 funs <- list(signal = DrawSignal, pca = DrawPCA)
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
35 if (type.draw %in% names(funs)) {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
36 fun <- funs[[type.draw]]
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
37 } else {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
38 stop(paste("Unknown type:", type.draw))
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
39 }
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
40
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
41 # Plot finalisation ----------------------------------------------
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
42 if (is.vector(Signal_data)) {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
43 Signal_data <- vec2mat(Signal_data)
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
44 }
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
45 fun(Signal_data, createWindow = createWindow, ...)
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
46 if (createFile) {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
47 grDevices::dev.off()
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
48 }
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
49 }
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
50
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
51
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
52
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
53 ##### DrawSignal
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
54
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
55 DrawSignal <- function(Signal_data, subtype = c("stacked", "together",
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
56 "separate", "diffmean", "diffmedian", "diffwith"),
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
57 ReImModArg = c(TRUE, FALSE, FALSE, FALSE), vertical = T,
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
58 xlab = "rowname", RowNames = NULL, row = 1, num.stacked = 4,
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
59 main = NULL, createWindow) {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
60 # nticks
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
61
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
62 # Data initialisation and checks ----------------------------------------------
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
63
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
64 subtype <- match.arg(subtype)
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
65 vec <- is.vector(Signal_data)
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
66 if (vec) {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
67 Signal_data <- vec2mat(Signal_data)
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
68 }
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
69
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
70 n <- nrow(Signal_data)
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
71 m <- ncol(Signal_data)
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
72
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
73 if (n < num.stacked){
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
74 num.stacked <- n
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
75 }
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
76
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
77 scale <- colnames(Signal_data)
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
78
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
79 num.plot <- sum(ReImModArg)
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
80
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
81 Var <- rowname <- value <- NULL # only for R CMD check
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
82
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
83 # Drawing array
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
84 if (num.plot <= 0) {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
85 stop("Nothing selected in ReImModArg.")
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
86 } else if (num.plot <= 2) {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
87 if (vertical) {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
88 nrow <- num.plot
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
89 ncol <- 1
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
90 } else {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
91 nrow <- 1
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
92 ncol <- num.plot
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
93 }
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
94 } else {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
95 nrow <- 2
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
96 ncol <- 2
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
97 }
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
98
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
99 # RowNames
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
100 if (is.null(RowNames)) {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
101 RowNames <- rownames(Signal_data)
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
102 if (is.null(RowNames)) {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
103 RowNames <- 1:n
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
104 }
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
105 } else {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
106 if (!is.vector(RowNames)) {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
107 stop("RowNames is not a vector")
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
108 }
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
109 if (length(RowNames) != n) {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
110 stop(paste("RowNames has length", length(RowNames), "and there are", n, "FIDs."))
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
111 }
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
112 }
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
113
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
114 if (n == 1) {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
115 RowNames <- deparse(substitute(Signal_data))
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
116 }
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
117
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
118 elements <- list()
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
119 if (ReImModArg[1]) {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
120 elements[["Re"]] <- Re(Signal_data)
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
121 rownames(elements[["Re"]]) <- RowNames
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
122 }
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
123 if (ReImModArg[2]) {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
124 elements[["Im"]] <- Im(Signal_data)
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
125 rownames(elements[["Im"]]) <- RowNames
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
126 }
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
127 if (ReImModArg[3]) {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
128 elements[["Mod"]] <- Mod(Signal_data)
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
129 rownames(elements[["Mod"]]) <- RowNames
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
130 }
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
131 if (ReImModArg[4]) {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
132 elements[["Arg"]] <- Arg(Signal_data)
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
133 rownames(elements[["Arg"]]) <- RowNames
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
134 }
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
135
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
136
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
137
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
138
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
139 # Drawing --------------------------------------------------------------------
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
140
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
141 y = x = NULL # only for R CMD check
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
142
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
143
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
144 # SEPARATE or STACKED ===============
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
145 if (subtype == "separate" | subtype == "stacked") {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
146
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
147 i <- 1
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
148 while (i <= n) {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
149 if (createWindow) {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
150 grDevices::dev.new(noRStudioGD = TRUE)
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
151 }
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
152 if (subtype == "separate") {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
153 # The other uses gridExtra to do that
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
154 graphics::par(mfrow = c(nrow, ncol))
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
155 }
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
156 plots <- list()
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
157 if (subtype == "separate") {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
158 last <- i
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
159 } else {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
160 last <- min(i + num.stacked - 1, n)
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
161 }
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
162 for (name in names(elements)) {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
163 if (subtype == "separate") {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
164 if (n == 1) {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
165 df <- data.frame(x = as.numeric(scale), y = elements[[name]])
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
166 } else {df <- data.frame(x = as.numeric(scale), y = elements[[name]][i, ])
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
167 }
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
168
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
169 plots[[name]] <- ggplot2::ggplot(data = df, ggplot2::aes(x = x, y = y)) +
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
170 ggplot2::geom_line(size = 1) +
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
171 ggplot2::theme(legend.position = "none") +
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
172 ggplot2::labs(x = xlab, y = name) +
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
173 ggplot2::ggtitle(RowNames[i]) +
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
174 ggplot2::theme_bw()
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
175
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
176 if ((df[1, "x"] - df[(dim(df)[1]), "x"]) > 0) {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
177 plots[[name]] <- plots[[name]] + ggplot2::scale_x_reverse()
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
178 }
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
179
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
180 } else {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
181
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
182 if (n == 1 ) {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
183 melted <- data.frame(rowname = rep(name, m),
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
184 Var = as.numeric(scale), value = elements[[name]][i,])
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
185 } else if (last==i){
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
186 melted <- data.frame(rowname = rep(rownames(elements[[name]])[i], m),
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
187 Var = as.numeric(scale), value = elements[[name]][i,])
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
188 } else {melted <- reshape2::melt(elements[[name]][i:last, ],
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
189 varnames = c("rowname", "Var"))
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
190 }
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
191
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
192
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
193 plots[[name]] <- ggplot2::ggplot(data = melted, ggplot2::aes(x = Var, y = value)) +
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
194 ggplot2::geom_line(size = 0.3) +
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
195 ggplot2::facet_grid(rowname ~ ., scales = "free_y") +
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
196 ggplot2::theme(legend.position = "none") +
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
197 ggplot2::labs(x = xlab, y = name) +
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
198 ggplot2::ggtitle(label = main) +
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
199 ggplot2::theme_bw()
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
200
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
201 if ((melted[1, "Var"] - melted[(dim(melted)[1]), "Var"]) > 0) {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
202 plots[[name]] <- plots[[name]] + ggplot2::scale_x_reverse()
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
203 }
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
204 }
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
205 }
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
206
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
207 if (subtype == "stacked") {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
208 do.call(gridExtra::grid.arrange, c(plots, list(nrow = nrow, ncol = ncol)))
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
209 }
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
210
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
211 i <- last + 1
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
212 }
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
213 } else if (subtype %in% c("together", "diffmean", "diffmedian", "diffwith")) {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
214
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
215 # TOGHETER or DIFFMEAN or DIFFMEDIAN or DIFFWITH ===============
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
216
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
217 rainbow_colors <- grDevices::rainbow(n)
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
218
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
219 if (createWindow) {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
220 grDevices::dev.new(noRStudioGD = TRUE)
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
221 }
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
222 graphics::par(mfrow = c(nrow, ncol))
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
223
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
224 plots <- list()
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
225
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
226 # Loop for Re, Im, Mod and Arg
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
227 for (name in names(elements)) {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
228 # Get this part of the signal
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
229 element <- elements[[name]]
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
230
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
231 # Express the signal according to a reference if asked by `subtype'
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
232 if (subtype == "diffmean") {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
233 element <- sweep(element, MARGIN = 2, colMeans(element), `-`)
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
234 } else if (subtype == "diffmedian") {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
235 element <- sweep(element, MARGIN = 2, matrixStats::colMedians(element), `-`)
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
236 } else if (subtype == "diffwith") {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
237 element <- sweep(element, MARGIN = 2, element[row, ], `-`)
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
238 if (row == 1 & n > 1) {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
239 # Since we use plot on the first row and lines on the following, the y
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
240 # scale is calculated at the first row so if the first row is all 0, it
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
241 # causes problems
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
242 tmp <- element[1, ]
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
243 element[1, ] <- element[2, ]
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
244 element[2, ] <- tmp
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
245 }
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
246 }
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
247
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
248
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
249 melted <- reshape2::melt(elements[[name]], varnames = c("rowname", "Var"))
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
250
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
251
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
252 plots[[name]] <- ggplot2::ggplot(melted, ggplot2::aes(x = Var,
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
253 y = value, group = rowname, colour = rowname)) + ggplot2::geom_line() +
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
254 ggplot2::labs(x = xlab, y = name) + ggplot2::scale_colour_discrete(name = NULL) +
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
255 ggplot2::ggtitle(main)
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
256
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
257 if ((melted[1, "Var"] - melted[(dim(melted)[1]), "Var"]) >
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
258 0) {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
259 plots[[name]] <- plots[[name]] + ggplot2::scale_x_reverse()
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
260 }
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
261
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
262 do.call(gridExtra::grid.arrange, c(plots, list(nrow = nrow,
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
263 ncol = ncol)))
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
264 }
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
265 }
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
266
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
267
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
268 }