-
Notifications
You must be signed in to change notification settings - Fork 1
/
plot_fd_new1.R
executable file
·205 lines (203 loc) · 6.31 KB
/
plot_fd_new1.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# 7/11/21 change color
plot.fd.new <- function (x, y, Lfdobj = 0, href = TRUE, titles = NULL, xlim = NULL,
ylim = NULL, xlab = NULL, ylab = NULL, ask = FALSE, nx = NULL,
axes = NULL, family = NULL, ...) {
fdobj <- x
if (!(is.fd(fdobj) || is.fdPar(fdobj)))
stop(paste("First argument is neither a functional data or a ",
"functional parameter object."))
if (is.fdPar(fdobj))
fdobj <- fdobj$fd
if (is.null(axes)) {
if (is.null(fdobj$basis$axes)) {
Axes <- TRUE
axFun <- FALSE
}
else {
if (!inherits(fdobj$basis$axes, "list"))
stop("fdobj$basis$axes must be a list; ",
"class(fdobj$basis$axes) = ", class(fdobj$basis$axes))
if (!(inherits(fdobj$basis$axes[[1]], "character") ||
inherits(fdobj$basis$axes[[1]], "function")))
stop("fdobj$basis$axes[[1]] must be either a function or the ",
"name of a function; class(fdobj$basis$axes[[1]]) = ",
class(fdobj$basis$axes[[1]]))
Axes <- FALSE
axFun <- TRUE
axList <- c(fdobj$basis$axes, ...)
}
}
else {
if (is.logical(axes)) {
Axes <- axes
axFun <- FALSE
}
else {
if (!inherits(axes, "list"))
stop("axes must be a logical or a list; class(axes) = ",
class(axes))
if (!(inherits(axes[[1]], "character") || inherits(axes[[1]],
"function")))
stop("axes[[1]] must be either a function or the ",
"name of a function; class(axes[[1]]) = ",
class(axes[[1]]))
Axes <- FALSE
axFun <- TRUE
axList <- c(axes, ...)
}
}
Lfdobj <- int2Lfd(Lfdobj)
if (!inherits(Lfdobj, "Lfd"))
stop("Second argument is not a linear differential operator.")
coef <- fdobj$coefs
coefd <- dim(coef)
ndim <- length(coefd)
nbasis <- coefd[1]
if (is.null(nx))
nx <- max(c(501, 10 * nbasis + 1))
nrep <- coefd[2]
if (ndim > 2)
nvar <- coefd[3]
else nvar <- 1
basisobj <- fdobj$basis
rangex <- basisobj$rangeval
if (missing(y)) {
y <- nx
}
else {
if (is.numeric(y))
y <- as.vector(y)
}
Y <- y
if (length(y) == 1) {
if (y >= 1) {
y <- seq(rangex[1], rangex[2], len = round(y))
}
else {
stop("'y' a single number less than one.")
}
}
if (min(y) < rangex[1] || max(y) > rangex[2])
stop("Values in Y are outside the basis range.")
if (is.null(xlim)) {
xlim <- rangex
}
else {
rangex[1] <- max(rangex[1], xlim[1])
rangex[2] <- min(rangex[2], xlim[2])
if (length(Y) == 1)
y <- seq(rangex[1], rangex[2], len = round(Y))
}
fdmat <- eval.fd(y, fdobj, Lfdobj)
rangey <- range(fdmat)
if (is.null(ylim))
ylim <- rangey
fdnames = fdobj$fdnames
fdlabelslist = fdlabels(fdnames, nrep, nvar)
xlabel = fdlabelslist$xlabel
ylabel = fdlabelslist$ylabel
casenames = fdlabelslist$casenames
varnames = fdlabelslist$varnames
if (is.null(xlab))
xlab <- xlabel
if (is.null(ylab))
ylab <- ylabel
if (ndim < 2) {
plot(y, fdmat, type = "l", xlim = xlim, ylim = ylim,
xlab = xlab, ylab = ylab, axes = Axes
, xaxt="n", ...)
# xaxis labels and locations
x.axis <- seq(from=0, to=24, by=1)
x.axis.lab <- paste0(x.axis,":00")
x.axis.loc <- x.axis/24*288
axis(1, at=x.axis.loc, labels=x.axis.lab, las=0)
if (axFun)
do.call(axList[[1]], axList[-1])
if (zerofind(fdmat) && href)
abline(h = 0, lty = 2)
}
if (ndim == 2) {
if (!ask) {
matplot(y, fdmat, type = "l", xlim = xlim,
ylim = ylim, xlab = xlab, ylab = ylab, axes = Axes
, xaxt="n"
#, col="blue1"
, family=family, ps=11
, ...
)
# xaxis labels and locations
# x.axis <- seq(from=0, to=24, by=1)
x.axis <- seq(from=0, to=24, by=4)
x.axis.lab <- paste0(x.axis,":00")
x.axis.loc <- x.axis/24*288
axis(1, at=x.axis.loc, labels=x.axis.lab, las=0)
if (axFun)
do.call(axList[[1]], axList[-1])
if (zerofind(fdmat) && href)
abline(h = 0, lty = 2)
}
else {
op <- par(ask = FALSE)
on.exit(par(op))
cat("Multiple plots: Click in the plot to advance to the next")
for (irep in 1:nrep) {
plot(y, fdmat[, irep], type = "l", xlim = xlim,
ylim = ylim, xlab = xlab, ylab = ylab, axes = Axes,
...)
if (axFun)
do.call(axList[[1]], axList[-1])
if (irep < 2)
par(ask = ask)
if (!is.null(casenames))
title(casenames[irep])
else title(paste("Case", irep))
if (zerofind(ylim) && href)
abline(h = 0, lty = 2)
}
}
}
if (ndim == 3) {
if (!ask) {
for (ivar in 1:nvar) {
matplot(y, fdmat[, , ivar], type = "l",
xlim = xlim, ylim = ylim, xlab = xlab, ylab = ylab,
ask = FALSE, axes = Axes, ...)
if (axFun)
do.call(axList[[1]], axList[-1])
if (!is.null(varnames))
title(varnames[ivar])
else title(paste("Variable", ivar))
if (zerofind(ylim) && href)
abline(h = 0, lty = 2)
}
}
else {
op <- par(ask = FALSE)
on.exit(par(op))
cat("Multiple plots: Click in the plot to advance to the next")
for (irep in 1:nrep) {
for (ivar in 1:nvar) {
plot(y, fdmat[, irep, ivar], type = "l",
xlim = xlim, ylim = ylim, xlab = xlab, ylab = ylab,
axes = Axes, ...)
if (axFun)
do.call(axList[[1]], axList[-1])
if (!is.null(casenames))
titlestr = casenames[irep]
else titlestr = paste("Case", irep)
if (!is.null(varnames)) {
titlestr = paste(titlestr, " ", varnames[ivar])
}
else {
titlestr = paste(titlestr, " ", "Variable",
ivar)
}
title(titlestr)
if (zerofind(ylim) && href)
abline(h = 0, lty = 2)
}
}
}
}
"done"
}