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

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