annotate nmr_bucketing/DrawSpec.R @ 2:3cd762aac7a4 draft

Uploaded
author marie-tremblay-metatoul
date Thu, 20 Apr 2017 08:53:46 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
1 drawSpec <- function (X, startP = -1, endP = -1, groupLabel = NULL, useLog = -1, highBound = -1, lowBound = -1,
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
2 xlab = NULL, ylab = NULL, main = NULL, nAxisPos = 4, offside = 0)
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
3 {
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
4 groupLabel_name = groupLabel
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
5 X = as.data.frame(X)
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
6 # colnames(X) = c(1:ncol(X))
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
7 X = as.matrix(X)
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
8 if (highBound != -1) {
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
9 for (i in 1:nrow(X)) {
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
10 myIndex = which(X[i, ] > highBound)
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
11 X[i, myIndex] = highBound
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
12 }
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
13 }
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
14 if (lowBound != -1) {
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
15 for (i in 1:nrow(X)) {
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
16 myIndex = which(X[i, ] < lowBound)
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
17 X[i, myIndex] = lowBound
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
18 }
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
19 }
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
20 if (is.null(groupLabel)) {
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
21 groupLabel = c(1:nrow(X))
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
22 groupLabel = as.factor(groupLabel)
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
23 }
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
24 else {
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
25 levels(groupLabel) = c(1:length(levels(groupLabel)))
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
26 }
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
27 if (startP == -1)
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
28 startP = 1
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
29 if (endP == -1)
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
30 endP = ncol(X)
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
31 if (is.null(xlab)) {
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
32 xlab = "index"
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
33 }
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
34 if (is.null(ylab)) {
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
35 ylab = "intensity"
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
36 }
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
37 if (is.null(main)) {
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
38 main = paste(" ", startP + offside, "-", endP + offside)
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
39 }
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
40 GraphRange <- c(startP:endP)
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
41 yn <- X[, GraphRange]
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
42 if (useLog != -1)
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
43 yn = log(yn)
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
44 if (length(yn) > ncol(X))
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
45 {
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
46 plot(yn[1, ], ylim = c(min(yn), max(yn)), type = "n", ylab = ylab, xlab = xlab, main = main, xaxt = "n")
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
47 tempVal = trunc(length(GraphRange)/nAxisPos)
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
48 xPos = c(0:nAxisPos) * tempVal
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
49 axis(1, at = xPos, labels = colnames(X)[xPos + startP + offside])
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
50 for (i in 1:length(levels(groupLabel)))
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
51 {
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
52 groupLabelIdx = which(groupLabel == levels(groupLabel)[i])
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
53 color <- palette(rainbow(length(levels(groupLabel))))
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
54 for (j in 1:length(groupLabelIdx))
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
55 {
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
56 lines(yn[groupLabelIdx[j], ], col = color[i])
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
57 }
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
58 }
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
59 if (!is.null(groupLabel_name))
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
60 {
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
61 legendPos = "topleft"
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
62 legend(legendPos, levels(groupLabel_name), col = as.integer(levels(groupLabel)), text.col = "black", pch = c(19, 19), bg = "gray90")
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
63 }
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
64 }
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
65 if (length(yn) == ncol(X))
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
66 {
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
67 plot(yn, ylim = c(min(yn), max(yn)), type = "n", ylab = ylab, xlab = xlab, main = main, xaxt = "n")
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
68 tempVal = trunc(length(GraphRange)/nAxisPos)
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
69 xPos = c(0:nAxisPos) * tempVal
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
70 # axis(1, at = xPos, labels = xPos + startP + offside)
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
71 axis(1, at = xPos, labels = colnames(X)[xPos + startP + offside])
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
72 lines(yn)
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
73 }
3cd762aac7a4 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
74 }