/
gr-Coe.R
executable file
·140 lines (135 loc) · 4.59 KB
/
gr-Coe.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
# 5. Coe / OutCoe / OpnCoe plotters
# ------------------------------------------
#' Boxplot of morphometric coefficients
#'
#' Explores the distribution of coefficient values.
#'
#' @param x the \link{Coe} object
#' @param ... useless here
#' @return a ggplot2 object
#' @aliases boxplot.Coe
#' @family Coe_graphics
#' @examples
#' # on OutCoe
#' bot %>% efourier(9) %>% rm_harm(1) %>% boxplot()
#'
#' data(olea)
#' op <- opoly(olea)
#' boxplot(op)
#' @export
boxplot.OutCoe <- function(x, ...){
# we convert to a data.frame
x$coe %>%
.melt_mat %>%
dplyr::mutate(coeff=factor(as.numeric(substr(key, 2, nchar(key)))),
harm=substr(key, 1, 1)) %>%
ggplot() +
aes(coeff, value) +
geom_boxplot() +
facet_grid(harm~., scales = "free_y")
}
#'@export
boxplot.OpnCoe <- function(x, ...){
# xfourier case should be treated as a Out
if (grepl("fourier", x$method)) {
return(boxplot.OutCoe(x))
}
# otherwise...
x$coe %>%
.melt_mat %>%
ggplot() +
aes(key, value) +
geom_boxplot()
}
#' Harmonic contribution to shape
#'
#' Calculates contribution of harmonics to shape. The amplitude of every coefficients
#' of a given harmonic is multiplied by the coefficients provided and the resulting
#' shapes are reconstructed and plotted. Naturally, only works on Fourier-based methods.
#' @param Coe a \code{\link{Coe}} object (either \code{OutCoe} or (soon) \code{OpnCoe})
#' @param id the id of a particular shape, otherwise working on the meanshape
#' @param harm.r range of harmonics on which to explore contributions
#' @param amp.r a vector of numeric for multiplying coefficients
#' @param main a title for the plot
#' @param xlab a title for the x-axis
#' @param ylab a title for the y-axis
#' @param ... additional parameter to pass to \code{\link{coo_draw}}
#' @rdname harm.contrib
#' @return a plot
#' @family Coe_graphics
#' @examples
#' data(bot)
#' bot.f <- efourier(bot, 12)
#' hcontrib(bot.f)
#' hcontrib(bot.f, harm.r=3:10, amp.r=1:8, col="grey20",
#' main="A huge panel")
#' @export
hcontrib <- function(Coe, ...){
UseMethod("hcontrib")
}
#' @rdname harm.contrib
#' @export
hcontrib.OutCoe <- function(Coe,
id,
harm.r,
amp.r = c(0, 0.5, 1, 2, 5, 10),
main="Harmonic contribution to shape",
xlab="Harmonic rank",
ylab="Amplification factor", ...){
x <- Coe
# we handle the method
p <- pmatch(tolower(x$method), c("efourier", "rfourier", "tfourier"))
if (is.na(p)) { warning("unvalid method. efourier is used")
} else {
method.i <- switch(p, efourier_i, rfourier_i, tfourier_i)}
# we deduce the number of coefficient / harmonic, and their number
cph <- ifelse(p==1, 4, 2)
nb.h <- ncol(x$coe)/cph
# we handle for missing harm.r
if (missing(harm.r)) harm.r <- 1:ifelse(nb.h > 6, 6, nb.h)
# if id is provided, we work on it, otherwise, on the average shape
if (missing(id)){
coe <- apply(x$coe, 2, mean)
message("no 'id' provided, working on the meanshape")
} else {
coe <- x$coe[id, ]}
# we prepare a xf to feed the method.i functions
xf <- coeff_split(coe, nb.h, cph)
# we prepare a neutral amplification factor
mult <- rep(1, nb.h)
# the core below
shp <- list()
p <- 1 # kinda dirty
# we loop over harm.r and amp.r by just multiplying xf
# by a given vector, all set to 1 except the harmonic that has to
# amplified
for (j in seq(along=harm.r)){
for (i in seq(along=amp.r)){
mult.loc <- mult
mult.loc[harm.r[j]] <- amp.r[i]
xfi <- lapply(xf, function(x) x*mult.loc)
shp[[p]] <- method.i(xfi)
p <- p+1}}
# graphics start here
# we borrow this block to PC.contrib
# except the expand.grid and coo_trans that needed to be "transposed"
xs <- 1:length(harm.r) - 0.5
ys <- rev(1:length(amp.r) - 0.5)
plot(NA, xlim=c(0, length(harm.r)), ylim=c(0, length(amp.r)),
asp=1, frame=FALSE, axes=FALSE,
main=main, xlab=xlab, ylab=ylab)
axis(1, at = xs, labels = harm.r)
axis(2, at = ys, labels = amp.r, las=1)
# we template the size of the shapes
shp <- lapply(shp, coo_close)
shp <- lapply(shp, coo_template, 0.95)
# here we prepare and apply the translation values
trans <- expand.grid(ys, xs)
colnames(trans) <- c("x", "y")
for (i in seq(along=shp)){
shp[[i]] <- coo_trans(shp[[i]], trans[i, 2], trans[i, 1])}
# we finally plot the shapes
gc <- lapply(shp, coo_draw, centroid = FALSE, first.point=FALSE, ...)
invisible(list(shp=shp, trans=trans))}
# hcontrib.Opn (dct) # todo
##### end graphics Coe