-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeAndRoll.other.functions.R
More file actions
296 lines (256 loc) · 9.85 KB
/
CodeAndRoll.other.functions.R
File metadata and controls
296 lines (256 loc) · 9.85 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
######################################################################
# Less often used R functions from CodeAndRoll.R
######################################################################
# source('/GitHub/Packages/CodeAndRoll/CodeAndRoll.other.functions.R')
wA4 = 8.27 # A4 inches
hA4 =11.69
# ------------------------------------------------------------------------
# ------------------------------------------------------------------------
wPairConnector <- function(DFrn=A3, PairAnnot=Sisters, verbose=FALSE, addlabels=FALSE, ...) { # Connect Pairs of datapoints with a line on a plot.
RN = rownames(DFrn)
stopifnot(length(RN) == NROW(DFrn))
Siz = Sisters[unique(RN)]
LS = splititsnames_byValues(Siz)
LengZ =unlapply(LS, length)
Tx = table(LengZ); Tx
if (verbose) iprint(paste(names(Tx), Tx, sep = ":", collapse = " and "))
LScool = LS[LengZ==2]
i =1
for (i in 1:length(LScool) ) {
P = LScool[[i]]
segments( DFrn[ P[1], 1], DFrn[ P[1], 2],
DFrn[ P[2], 1], DFrn[ P[2], 2], ...)
} #for
if (addlabels) text( DFrn, labels = Sisters[RN], srt=65, cex=.75, pos=4)
}
# ------------------------------------------------------------------------
my_render <- function(input, encoding) { # For Rmarkdown to keep the markdown file after rendering. Source: https://github.com/rstudio/rmarkdown/issues/107
rmarkdown::render(input, clean = FALSE, encoding = encoding)
}
# Then add this to your metadata:
# output: html_document
# knit: my_render
# ------------------------------------------------------------------------
#' whist.back2back
#'
#' Two back-to-back histograms from a list. The X-axis is only correct if breaks1 ==breaks2.
#' Undeveloped function, contains graphical bugs, no support for this function.
#' @param ListOf2 List of 2 numeric vectors
#' @param breaks1 break parameter for histogram function for the 2st list element.
#' @param breaks2 break parameter for histogram function for the 2st list element.
#' @param ... Pass any other parameter of the corresponding
#' plotting function(most of them should work).
#' @param plotname The name of the file saved.
#' @param main The title of the plot.
#' @param ylab Y-axis label
#' @param col Color of the 2 histograms
#' @param incrBottMarginBy Increase the blank space at the bottom of the plot. Use if labels do not
#' fit on the plot.
#' @param savefile Save plot as pdf in OutDir, TRUE by default.
#' @param w Width of the saved pdf image, in inches.
#' @param h Height of the saved pdf image, in inches.
#' @param mdlink Insert a .pdf and a .png image link in the markdown report,
#' set by "path_of_report".
#' @param PNG Set to true if you want to save the plot as PNG instead of the default PDF.
#' @export
#' @examples try(dev.off(), silent = TRUE)
#' ListOf2 = list("A" = rnorm(100), "B"=rnorm(100))
#' ls_of_hists = whist.back2back(ListOf2)
whist.back2back <-
function(ListOf2 = list("A" = rnorm(10000), "B" = rnorm(10000)),
breaks1 = 20,
breaks2 = breaks1,
col = c("green", "blue"),
plotname = substitute(variable),
main = plotname,
ylab = "Frequency",
savefile = UnlessSpec("b.save.wplots"),
incrBottMarginBy = 0,
w = UnlessSpec("b.defSize", 7),
h = w,
mdlink = ww.set.mdlink(),
PNG = UnlessSpec("b.usepng"),
...) {
print("Does not always work - experimental. Problem is the separate binning.")
fname = kollapse(plotname, ".hist.btb")
if (incrBottMarginBy) {
.ParMarDefault <- par("mar")
par(mar = c(par("mar")[1] + incrBottMarginBy, par("mar")[2:4]))
} # Tune the margin
lsNm = if (!is.null(names(ListOf2)))
names(ListOf2)
else
1:2
lng = length(ListOf2)
if (lng != 2) {
iprint("length(List): ", lng, " First two elements used")
} #if
x = NULL
x[[1]] = h1 = hist(ListOf2[[1]], plot = FALSE, breaks = breaks1)
x[[2]] = h2 = hist(ListOf2[[2]], plot = FALSE, breaks = breaks2)
names(h2$counts) = h2$breaks[-1]
names(h1$counts) = h1$breaks[-1]
h2$counts = -h2$counts
AllBreaks = sort(union(h1$breaks, h2$breaks))[-1]
ct1 = h1$counts[as.character(AllBreaks)]
ct2 = h2$counts[as.character(AllBreaks)]
hmax = max(h1$counts, na.rm = TRUE)
hmin = min(h2$counts, na.rm = TRUE)
xlimm = range(unlist(ListOf2), na.rm = TRUE)
xlimm = c(1, max(length(h2$counts), length(h1$counts)) + 3)
colorz = col # to avoid circular reference in the inside function argument
main_ = main
barplot(
ct1,
ylim = c(hmin, hmax),
xlim = xlimm,
col = colorz[1],
names.arg = AllBreaks,
las = 3,
main = main_,
ylab = ylab,
...
)
barplot(ct2,
col = colorz[2],
add = TRUE,
names.arg = "")
legend("topright", lsNm[1], bty = "n")
legend("bottomright", lsNm[2], bty = "n")
if (savefile) {
ww.dev.copy(
PNG_ = PNG,
fname_ = fname,
w_ = w,
h_ = h
)
}
if (incrBottMarginBy) {
par("mar" = .ParMarDefault)
}
assign("plotnameLastPlot", fname, envir = .GlobalEnv)
if (mdlink & savefile) {
ww.MarkDown_Img_Logger_PDF_and_PNG(fname_wo_ext = fname)
}
x
}
# ------------------------------------------------------------------------
#' corner.label.w
#'
#' Add Legends to the corners. From the Plotrix package.
#' @param label Text to display
#' @param cex font size
#' @param x an integer value: -1 for the left side of the plot, 1 for the right side
#' @param y an integer value: -1 for the bottom side of the plot, 1 for the top side
#' @param `xoff,yoff` Horizontal and vertical text offsets. If NA,
#' it defaults to one half of the width and height of "m" respectively.
#' @param figcorner Whether to find/display at the corner of the plot or figure.
#' @export
#' @examples plot(2); corner.label.w("A")
corner.label.w <- function(label = "A", # Add Legends to the corners. From the Plotrix package.
cex = 3,
x = -1,
y = 1,
xoff = 1,
yoff = 1
,
figcorner = TRUE,
...) {
if (is.na(xoff))
xoff <- strwidth("m") / 2
if (is.na(yoff))
yoff <- strheight("m") / 2
par.usr <- par("usr")
xpos <- par.usr[(3 + x) / 2]
ypos <- par.usr[(3 + y) / 2 + 2]
if (figcorner) {
par.pin <- par("pin")
xplotrange <- par.usr[2] - par.usr[1]
yplotrange <- par.usr[4] - par.usr[3]
par.mai <- par("mai")
xmar <- xplotrange * par.mai[3 + x] / par.pin[1]
ymar <- yplotrange * par.mai[2 + y] / par.pin[2]
xpos <- xpos + x * xmar
ypos <- ypos + y * ymar
}
if (!is.null(label)) {
if (figcorner)
par(xpd = TRUE)
text(xpos - x * xoff, ypos - y * yoff, label, adj = c((1 + x) / 2, (1 + y) /
2), cex, ...)
if (figcorner)
par(xpd = FALSE)
}
return(list(x = xpos, y = ypos))
}
# unrequire <- function(string='MarkdownReportsDev') detach(name = paste0("package:",string, collapse = ""), unload=TRUE)
# unrequire()
# detach("package:MarkdownReportsDev", unload=TRUE)
# require('MarkdownReportsDev' )
# unrequire('MarkdownReportsDev' )
# TMP
# create_set_OutDir <- function(..., setDir = TRUE) { # create set OutDir TMP for markdownreports
# OutDir = kollapse(..., print = FALSE)
# if (!substrRight(OutDir, 1) == "/")
# OutDir = paste0(OutDir, "/") # add '/' if necessary
# OutDir = gsub(x = OutDir,
# pattern = '//',
# replacement = '/')
# iprint("All files will be saved under 'OutDir': ", OutDir)
# if (!exists(OutDir)) {
# dir.create(OutDir, showWarnings = FALSE)
# }
# if (setDir) {
# setwd(OutDir)
# }
# ww.assign_to_global("OutDir", OutDir, 1)
# }
# # primitive, legacy function
# list2df.unordered <- function(L) { # When converting a list to a data frame, the list elements can have different lengths. This function fills up the data frame with NA values.
# maxlen <- max(sapply(L, length))
# do.call(data.frame, lapply(L, pad.na, len=maxlen))
# }
# # list2df.unordered = list2df_NA_padded
#
# list2df <- function(your_list ) { do.call(cbind.data.frame, your_list)} # Basic list-to-df functionality in R
#
# list2df_presence <- function(yalist, entries_list = FALSE, matrixfill = "") { # Convert a list to a full dataframe, summarizing the presence or absence of elements
# if( is.null(names(yalist)) ) {names(yalist) = 1:length(yalist)}
#
# rown = unique(unlist(yalist))
# coln = names(yalist)
# mm = matrix.fromNames(rown, coln, fill = matrixfill)
# entries_list = lapply(yalist, names)
#
# for (i in 1:length(yalist)) {
# print(i)
# le = unlist(yalist[i])
# names(le) = unlist(entries_list[i])
#
# list_index = which( le %in% rown)
# m_index = which( rown %in% le)
# mm[ m_index, i] = names(le[list_index])
# }
# return(mm)
# }
#
#
# list2fullDF <- function(ll, byRow=TRUE){ # convert a list to a full numeric data matrix. Designed for occurence counting, think tof table()
# entrytypes = unique(unlist(lapply(ll, names)))
# ls_len = length(ll)
# mat =matrix(0, ncol = ls_len, nrow = length(entrytypes))
# colnames(mat) = if (length(names(ll))) names(ll) else 1:ls_len
# rownames(mat) = sort(entrytypes)
# for (i in 1:length(ll)) {
# mat[names(ll[[i]]) , i] = ll[[i]]
# print(names(ll[[i]]))
# }
# if(!byRow) {mat = t(mat)}
# return(mat)
# }
# list_to_fullDF = list2fullDF
# sortbyitsnames <- function(vec_or_list) { # Sort a vector by the alphanumeric order of its names (instead of its values).
# print("THIS FUCNTION MAKES MISTAKES WITH DUPLICATE NAMES")
# if (is.vector(vec_or_list) & !is.list(vec_or_list)) { vec[gtools::mixedsort(names(vec_or_list) )]
# } else if (is.list(vec_or_list)) { reorder.list(L = (vec_or_list), namesOrdered = gtools::mixedsort(names(vec_or_list))) }
# }