/
plot_KDE.R
1251 lines (1088 loc) · 40.7 KB
/
plot_KDE.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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#' Plot kernel density estimate with statistics
#'
#' Plot a kernel density estimate of measurement values in combination with the
#' actual values and associated error bars in ascending order. If enabled, the
#' boxplot will show the usual distribution parameters (median as
#' bold line, box delimited by the first and third quartile, whiskers defined
#' by the extremes and outliers shown as points) and also the mean and
#' standard deviation as pale bold line and pale polygon, respectively.
#'
#' The function allows passing several plot arguments, such as `main`,
#' `xlab`, `cex`. However, as the figure is an overlay of two
#' separate plots, `ylim` must be specified in the order: c(ymin_axis1,
#' ymax_axis1, ymin_axis2, ymax_axis2) when using the cumulative values plot
#' option. See examples for some further explanations. For details on the
#' calculation of the bin-width (parameter `bw`) see
#' [density].
#'
#'
#' A statistic summary, i.e. a collection of statistic measures of
#' centrality and dispersion (and further measures) can be added by specifying
#' one or more of the following keywords:
#' - `"n"` (number of samples)
#' - `"mean"` (mean De value)
#' - `"median"` (median of the De values)
#' - `"sd.rel"` (relative standard deviation in percent)
#' - `"sd.abs"` (absolute standard deviation)
#' - `"se.rel"` (relative standard error)
#' - `"se.abs"` (absolute standard error)
#' - `"in.2s"` (percent of samples in 2-sigma range)
#' - `"kurtosis"` (kurtosis)
#' - `"skewness"` (skewness)
#'
#'
#' **Note** that the input data for the statistic summary is sent to the function
#' `calc_Statistics()` depending on the log-option for the z-scale. If
#' `"log.z = TRUE"`, the summary is based on the logarithms of the input
#' data. If `"log.z = FALSE"` the linearly scaled data is used.
#'
#' **Note** as well, that `"calc_Statistics()"` calculates these statistic
#' measures in three different ways: `unweighted`, `weighted` and
#' `MCM-based` (i.e., based on Monte Carlo Methods). By default, the
#' MCM-based version is used. If you wish to use another method, indicate this
#' with the appropriate keyword using the argument `summary.method`.
#'
#'
#' @param data [data.frame] or [RLum.Results-class] object (**required**):
#' for `data.frame`: two columns: De (`values[,1]`) and De error (`values[,2]`).
#' For plotting multiple data sets, these must be provided as
#' `list` (e.g. `list(dataset1, dataset2)`).
#'
#' @param na.rm [logical] (*with default*):
#' exclude NA values from the data set prior to any further operation.
#'
#' @param values.cumulative [logical] (*with default*):
#' show cumulative individual data.
#'
#' @param order [logical]:
#' Order data in ascending order.
#'
#' @param boxplot [logical] (*with default*):
#' optionally show a boxplot (depicting median as thick central line,
#' first and third quartile as box limits, whiskers denoting +/- 1.5
#' interquartile ranges and dots further outliers).
#'
#' @param rug [logical] (*with default*):
#' optionally add rug.
#'
#' @param summary [character] (*optional*):
#' add statistic measures of centrality and dispersion to the plot. Can be one
#' or more of several keywords. See details for available keywords.
#'
#' @param summary.pos [numeric] or [character] (*with default*):
#' optional position coordinates or keyword (e.g. `"topright"`)
#' for the statistical summary. Alternatively, the keyword `"sub"` may be
#' specified to place the summary below the plot header. However, this latter
#' option in only possible if `mtext` is not used. In case of coordinate
#' specification, y-coordinate refers to the right y-axis.
#'
#' @param summary.method [character] (*with default*):
#' keyword indicating the method used to calculate the statistic summary.
#' One out of `"unweighted"`, `"weighted"` and `"MCM"`.
#' See [calc_Statistics] for details.
#'
#' @param bw [character] (*with default*):
#' bin-width, chose a numeric value for manual setting.
#'
#' @param output [logical]:
#' Optional output of numerical plot parameters. These can be useful to
#' reproduce similar plots. Default is `TRUE`.
#'
#' @param ... further arguments and graphical parameters passed to [plot].
#'
#' @note
#' The plot output is no 'probability density' plot (cf. the discussion
#' of Berger and Galbraith in Ancient TL; see references)!
#'
#' @section Function version: 3.6.0
#'
#' @author
#' Michael Dietze, GFZ Potsdam (Germany)\cr
#' Geography & Earth Sciences, Aberystwyth University (United Kingdom)
#'
#' @seealso [density], [plot]
#'
#' @examples
#'
#' ## read example data set
#' data(ExampleData.DeValues, envir = environment())
#' ExampleData.DeValues <-
#' Second2Gray(ExampleData.DeValues$BT998, c(0.0438,0.0019))
#'
#' ## create plot straightforward
#' plot_KDE(data = ExampleData.DeValues)
#'
#' ## create plot with logarithmic x-axis
#' plot_KDE(data = ExampleData.DeValues,
#' log = "x")
#'
#' ## create plot with user-defined labels and axes limits
#' plot_KDE(data = ExampleData.DeValues,
#' main = "Dose distribution",
#' xlab = "Dose (s)",
#' ylab = c("KDE estimate", "Cumulative dose value"),
#' xlim = c(100, 250),
#' ylim = c(0, 0.08, 0, 30))
#'
#' ## create plot with boxplot option
#' plot_KDE(data = ExampleData.DeValues,
#' boxplot = TRUE)
#'
#' ## create plot with statistical summary below header
#' plot_KDE(data = ExampleData.DeValues,
#' summary = c("n", "median", "skewness", "in.2s"))
#'
#' ## create plot with statistical summary as legend
#' plot_KDE(data = ExampleData.DeValues,
#' summary = c("n", "mean", "sd.rel", "se.abs"),
#' summary.pos = "topleft")
#'
#' ## split data set into sub-groups, one is manipulated, and merge again
#' data.1 <- ExampleData.DeValues[1:15,]
#' data.2 <- ExampleData.DeValues[16:25,] * 1.3
#' data.3 <- list(data.1, data.2)
#'
#' ## create plot with two subsets straightforward
#' plot_KDE(data = data.3)
#'
#' ## create plot with two subsets and summary legend at user coordinates
#' plot_KDE(data = data.3,
#' summary = c("n", "median", "skewness"),
#' summary.pos = c(110, 0.07),
#' col = c("blue", "orange"))
#'
#' ## example of how to use the numerical output of the function
#' ## return plot output to draw a thicker KDE line
#' KDE_out <- plot_KDE(data = ExampleData.DeValues,
#' output = TRUE)
#'
#' @md
#' @export
plot_KDE <- function(
data,
na.rm = TRUE,
values.cumulative = TRUE,
order = TRUE,
boxplot = TRUE,
rug = TRUE,
summary,
summary.pos,
summary.method = "MCM",
bw = "nrd0",
output = TRUE,
...
) {
## check data and parameter consistency -------------------------------------
## account for depreciated arguments
if("centrality" %in% names(list(...))) {
boxplot <- TRUE
warning(paste("[plot_KDE()] Argument 'centrality' no longer supported. ",
"Replaced by 'boxplot = TRUE'."))
}
if("dispersion" %in% names(list(...))) {
boxplot <- TRUE
warning(paste("[plot_KDE()] Argument 'dispersion' no longer supported. ",
"Replaced by 'boxplot = TRUE'."))
}
if("polygon.col" %in% names(list(...))) {
boxplot <- TRUE
warning(paste("[plot_KDE()] Argument 'polygon.col' no longer supported. ",
"Replaced by 'boxplot = TRUE'."))
}
if("weights" %in% names(list(...))) {
warning(paste("[plot_KDE()] Argument 'weights' no longer supported. ",
"Weights are omitted."))
}
## Homogenise input data format
if(is(data, "list") == FALSE) {
data <- list(data)
}
## check/adjust input data structure
for(i in 1:length(data)) {
if(is(data[[i]], "RLum.Results") == FALSE &
is(data[[i]], "data.frame") == FALSE &
is.numeric(data[[i]]) == FALSE) {
stop(paste("[plot_KDE()] Input data format is neither",
"'data.frame', 'RLum.Results' nor 'numeric'"), call. = FALSE)
} else {
##extract RLum.Results
if(is(data[[i]], "RLum.Results") == TRUE) {
data[[i]] <- get_RLum(data[[i]], "data")[,1:2]
}
##make sure we only take the first two columns
data[[i]] <- data[[i]][,1:2]
##account for very short datasets
if(length(data[[i]]) < 2) {
data[[i]] <- cbind(data[[i]], rep(NA, length(data[[i]])))
}
}
##check for Inf values and remove them if need
if(any(is.infinite(unlist(data[[i]])))){
Inf_id <- which(is.infinite(unlist(data[[i]]))[1:nrow(data[[i]])/ncol(data[[i]])])
warning(paste("[plot_KDE()] Inf values removed in row(s):", paste(Inf_id, collapse = ", "), "in data.frame", i), call. = FALSE)
data[[i]] <- data[[i]][-Inf_id,]
rm(Inf_id)
##check if empty
if(nrow(data[[i]]) == 0){
data[i] <- NULL
}
}
}
##check if list is empty
if(length(data) == 0)
stop("[plot_KDE()] Your input is empty, intentionally or maybe after Inf removal? Nothing plotted!", call. = FALSE)
## check/set function parameters
if(missing(summary) == TRUE) {
summary <- ""
}
if(missing(summary.pos) == TRUE) {
summary.pos <- "sub"
}
## set mtext output
if("mtext" %in% names(list(...))) {
mtext <- list(...)$mtext
} else {
mtext <- ""
}
## check/set layout definitions
if("layout" %in% names(list(...))) {
layout <- get_Layout(layout = list(...)$layout)
} else {
layout <- get_Layout(layout = "default")
}
## data preparation steps ---------------------------------------------------
## optionally, count and exclude NA values and print result
if(na.rm == TRUE) {
for(i in 1:length(data)) {
n.NA <- sum(is.na(data[[i]][,1]))
if(n.NA == 1) {
message(paste("1 NA value excluded from data set", i, "."))
} else if(n.NA > 1) {
message(paste(n.NA, "NA values excluded from data set", i, "."))
}
data[[i]] <- na.exclude(data[[i]])
}
}
## optionally, order data set
if(order == TRUE) {
for(i in 1:length(data)) {
data[[i]] <- data[[i]][order(data[[i]][,1]),]
}
}
## calculate and paste statistical summary
De.stats <- matrix(nrow = length(data), ncol = 12)
colnames(De.stats) <- c("n",
"mean",
"median",
"kde.max",
"sd.abs",
"sd.rel",
"se.abs",
"se.rel",
"q.25",
"q.75",
"skewness",
"kurtosis")
De.density <- list(NA)
## loop through all data sets
for(i in 1:length(data)) {
statistics <- calc_Statistics(data[[i]], na.rm = na.rm)[[summary.method]]
De.stats[i,1] <- statistics$n
De.stats[i,2] <- statistics$mean
De.stats[i,3] <- statistics$median
De.stats[i,5] <- statistics$sd.abs
De.stats[i,6] <- statistics$sd.rel
De.stats[i,7] <- statistics$se.abs
De.stats[i,8] <- statistics$se.rel
De.stats[i,9] <- quantile(data[[i]][,1], 0.25)
De.stats[i,10] <- quantile(data[[i]][,1], 0.75)
De.stats[i,11] <- statistics$skewness
De.stats[i,12] <- statistics$kurtosis
if(nrow(data[[i]]) >= 2){
De.density[[length(De.density) + 1]] <- density(data[[i]][,1],
kernel = "gaussian",
bw = bw)
}else{
De.density[[length(De.density) + 1]] <- NA
warning("[plot_KDE()] Less than 2 points provided, no density plotted.", call. = FALSE)
}
}
## remove dummy list element
De.density[[1]] <- NULL
## create global data set
De.global <- data[[1]][,1]
De.error.global <- data[[1]][,2]
De.density.range <- matrix(nrow = length(data),
ncol = 4)
for(i in 1:length(data)) {
##global De and De.error vector
De.global <- c(De.global, data[[i]][,1])
De.error.global <- c(De.error.global, data[[i]][,2])
## density range
if(!all(is.na(De.density[[i]]))){
De.density.range[i,1] <- min(De.density[[i]]$x)
De.density.range[i,2] <- max(De.density[[i]]$x)
De.density.range[i,3] <- min(De.density[[i]]$y)
De.density.range[i,4] <- max(De.density[[i]]$y)
## position of maximum KDE value
De.stats[i,4] <- De.density[[i]]$x[which.max(De.density[[i]]$y)]
}else{
De.density.range[i,1:4] <- NA
De.stats[i,4] <- NA
}
}
## Get global range of densities
De.density.range <- c(min(De.density.range[,1]),
max(De.density.range[,2]),
min(De.density.range[,3]),
max(De.density.range[,4]))
label.text = list(NA)
if(summary.pos[1] != "sub") {
n.rows <- length(summary)
for(i in 1:length(data)) {
stops <- paste(rep("\n", (i - 1) * n.rows), collapse = "")
summary.text <- character(0)
for(j in 1:length(summary)) {
summary.text <- c(summary.text,
paste(
"",
ifelse("n" %in% summary[j] == TRUE,
paste("n = ",
De.stats[i,1],
"\n",
sep = ""),
""),
ifelse("mean" %in% summary[j] == TRUE,
paste("mean = ",
round(De.stats[i,2], 2),
"\n",
sep = ""),
""),
ifelse("median" %in% summary[j] == TRUE,
paste("median = ",
round(De.stats[i,3], 2),
"\n",
sep = ""),
""),
ifelse("kde.max" %in% summary[j] == TRUE,
paste("kdemax = ",
round(De.stats[i,4], 2),
" \n ",
sep = ""),
""),
ifelse("sd.abs" %in% summary[j] == TRUE,
paste("sd = ",
round(De.stats[i,5], 2),
"\n",
sep = ""),
""),
ifelse("sd.rel" %in% summary[j] == TRUE,
paste("rel. sd = ",
round(De.stats[i,6], 2), " %",
"\n",
sep = ""),
""),
ifelse("se.abs" %in% summary[j] == TRUE,
paste("se = ",
round(De.stats[i,7], 2),
"\n",
sep = ""),
""),
ifelse("se.rel" %in% summary[j] == TRUE,
paste("rel. se = ",
round(De.stats[i,8], 2), " %",
"\n",
sep = ""),
""),
ifelse("skewness" %in% summary[j] == TRUE,
paste("skewness = ",
round(De.stats[i,11], 2),
"\n",
sep = ""),
""),
ifelse("kurtosis" %in% summary[j] == TRUE,
paste("kurtosis = ",
round(De.stats[i,12], 2),
"\n",
sep = ""),
""),
ifelse("in.2s" %in% summary[j] == TRUE,
paste("in 2 sigma = ",
round(sum(data[[i]][,1] >
(De.stats[i,2] - 2 *
De.stats[i,5]) &
data[[i]][,1] <
(De.stats[i,2] + 2 *
De.stats[i,5])) /
nrow(data[[i]]) * 100 , 1),
" %",
sep = ""),
""),
sep = ""))
}
summary.text <- paste(summary.text, collapse = "")
label.text[[length(label.text) + 1]] <- paste(stops,
summary.text,
stops,
sep = "")
}
} else {
for(i in 1:length(data)) {
summary.text <- character(0)
for(j in 1:length(summary)) {
summary.text <- c(summary.text,
ifelse("n" %in% summary[j] == TRUE,
paste("n = ",
De.stats[i,1],
" | ",
sep = ""),
""),
ifelse("mean" %in% summary[j] == TRUE,
paste("mean = ",
round(De.stats[i,2], 2),
" | ",
sep = ""),
""),
ifelse("median" %in% summary[j] == TRUE,
paste("median = ",
round(De.stats[i,3], 2),
" | ",
sep = ""),
""),
ifelse("kde.max" %in% summary[j] == TRUE,
paste("kdemax = ",
round(De.stats[i,4], 2),
" | ",
sep = ""),
""),
ifelse("sd.rel" %in% summary[j] == TRUE,
paste("rel. sd = ",
round(De.stats[i,6], 2), " %",
" | ",
sep = ""),
""),
ifelse("sd.abs" %in% summary[j] == TRUE,
paste("abs. sd = ",
round(De.stats[i,5], 2),
" | ",
sep = ""),
""),
ifelse("se.rel" %in% summary[j] == TRUE,
paste("rel. se = ",
round(De.stats[i,8], 2), " %",
" | ",
sep = ""),
""),
ifelse("se.abs" %in% summary[j] == TRUE,
paste("abs. se = ",
round(De.stats[i,7], 2),
" | ",
sep = ""),
""),
ifelse("skewness" %in% summary[j] == TRUE,
paste("skewness = ",
round(De.stats[i,11], 2),
" | ",
sep = ""),
""),
ifelse("kurtosis" %in% summary[j] == TRUE,
paste("kurtosis = ",
round(De.stats[i,12], 2),
" | ",
sep = ""),
""),
ifelse("in.2s" %in% summary[j] == TRUE,
paste("in 2 sigma = ",
round(sum(data[[i]][,1] >
(De.stats[i,2] - 2 *
De.stats[i,5]) &
data[[i]][,1] <
(De.stats[i,2] + 2 *
De.stats[i,5])) /
nrow(data[[i]]) * 100 , 1),
" % ",
sep = ""),
"")
)
}
summary.text <- paste(summary.text, collapse = "")
label.text[[length(label.text) + 1]] <- paste(
" ",
summary.text,
sep = "")
}
## remove outer vertical lines from string
for(i in 2:length(label.text)) {
label.text[[i]] <- substr(x = label.text[[i]],
start = 3,
stop = nchar(label.text[[i]]) - 3)
}
}
## remove dummy list element
label.text[[1]] <- NULL
## read out additional parameters -------------------------------------------
if("main" %in% names(list(...))) {
main <- list(...)$main
} else {
main <- expression(bold(paste(D[e], " distribution")))
}
if("sub" %in% names(list(...))) {
sub <- list(...)$sub
} else {
sub <- NULL
}
if("xlab" %in% names(list(...))) {
xlab <- list(...)$xlab
} else {
xlab <- expression(paste(D[e], " [Gy]"))
}
if("ylab" %in% names(list(...))) {
ylab <- list(...)$ylab
} else {
ylab <- c("Density", "Cumulative frequency")
}
if("xlim" %in% names(list(...))) {
xlim.plot <- list(...)$xlim
} else {
xlim.plot <- c(min(c(De.global - De.error.global),
De.density.range[1],
na.rm = TRUE),
max(c(De.global + De.error.global),
De.density.range[2],
na.rm = TRUE))
}
if("ylim" %in% names(list(...))) {
ylim.plot <- list(...)$ylim
} else {
if(!is.na(De.density.range[1])){
ylim.plot <- c(De.density.range[3],
De.density.range[4],
0,
max(De.stats[,1]))
}else{
ylim.plot <- c(0,
max(De.stats[,1]),
0,
max(De.stats[,1]))
}
}
if("log" %in% names(list(...))) {
log.option <- list(...)$log
} else {
log.option <- ""
}
if("col" %in% names(list(...))) {
col.main <- list(...)$col
col.xlab <- 1
col.ylab1 <- 1
col.ylab2 <- 1
col.xtck <- 1
col.ytck1 <- 1
col.ytck2 <- 1
col.box <- 1
col.mtext <- 1
col.stats <- list(...)$col
col.kde.line <- list(...)$col
col.kde.fill <- NA
col.value.dot <- list(...)$col
col.value.bar <- list(...)$col
col.value.rug <- list(...)$col
col.boxplot <- list(...)$col
col.boxplot.line <- list(...)$col
col.boxplot.fill <- NA
col.mean.line <- adjustcolor(col = list(...)$col,
alpha.f = 0.4)
col.sd.bar <- adjustcolor(col = list(...)$col,
alpha.f = 0.4)
col.background <- NA
} else {
if(length(layout$kde$colour$main) == 1) {
col.main <- c(layout$kde$colour$main, 2:length(data))
} else {
col.main <- layout$kde$colour$main
}
if(length(layout$kde$colour$xlab) == 1) {
col.xlab <- c(layout$kde$colour$xlab, 2:length(data))
} else {
col.xlab <- layout$kde$colour$xlab
}
if(length(layout$kde$colour$ylab1) == 1) {
col.ylab1 <- c(layout$kde$colour$ylab1, 2:length(data))
} else {
col.ylab1 <- layout$kde$colour$ylab1
}
if(length(layout$kde$colour$ylab2) == 1) {
col.ylab2 <- c(layout$kde$colour$ylab2, 2:length(data))
} else {
col.ylab2 <- layout$kde$colour$ylab2
}
if(length(layout$kde$colour$xtck) == 1) {
col.xtck <- c(layout$kde$colour$xtck, 2:length(data))
} else {
col.xtck <- layout$kde$colour$xtck
}
if(length(layout$kde$colour$ytck1) == 1) {
col.ytck1 <- c(layout$kde$colour$ytck1, 2:length(data))
} else {
col.ytck1 <- layout$kde$colour$ytck1
}
if(length(layout$kde$colour$ytck2) == 1) {
col.ytck2 <- c(layout$kde$colour$ytck2, 2:length(data))
} else {
col.ytck2 <- layout$kde$colour$ytck2
}
if(length(layout$kde$colour$box) == 1) {
col.box <- c(layout$kde$colour$box, 2:length(data))
} else {
col.box <- layout$kde$colour$box
}
if(length(layout$kde$colour$mtext) == 1) {
col.mtext <- c(layout$kde$colour$mtext, 2:length(data))
} else {
col.mtext <- layout$kde$colour$mtext
}
if(length(layout$kde$colour$stats) == 1) {
col.stats <- c(layout$kde$colour$stats, 2:length(data))
} else {
col.stats <- layout$kde$colour$stats
}
if(length(layout$kde$colour$kde.line) == 1) {
col.kde.line <- c(layout$kde$colour$kde.line, 2:length(data))
} else {
col.kde.line <- layout$kde$colour$kde.line
}
if(length(layout$kde$colour$kde.fill) == 1) {
col.kde.fill <- c(layout$kde$colour$kde.fill, 2:length(data))
} else {
col.kde.fill <- layout$kde$colour$kde.fill
}
if(length(layout$kde$colour$value.dot) == 1) {
col.value.dot <- c(layout$kde$colour$value.dot, 2:length(data))
} else {
col.value.dot <- layout$kde$colour$value.dot
}
if(length(layout$kde$colour$value.bar) == 1) {
col.value.bar <- c(layout$kde$colour$value.bar, 2:length(data))
} else {
col.value.bar <- layout$kde$colour$value.bar
}
if(length(layout$kde$colour$value.rug) == 1) {
col.value.rug <- c(layout$kde$colour$value.rug, 2:length(data))
} else {
col.value.rug <- layout$kde$colour$value.rug
}
if(length(layout$kde$colour$boxplot.line) == 1) {
col.boxplot.line <- c(layout$kde$colour$boxplot.line, 2:length(data))
} else {
col.boxplot.line <- layout$kde$colour$boxplot.line
}
if(length(layout$kde$colour$boxplot.fill) == 1) {
col.boxplot.fill <- c(layout$kde$colour$boxplot.fill, 2:length(data))
} else {
col.boxplot.fill <- layout$kde$colour$boxplot.fill
}
if(length(layout$kde$colour$mean.line) == 1) {
col.mean.line <- adjustcolor(col = 1:length(data),
alpha.f = 0.4)
} else {
col.mean.line <- layout$kde$colour$mean.point
}
if(length(layout$kde$colour$sd.bar) == 1) {
col.sd.bar <- c(layout$kde$colour$sd.bar, 2:length(data))
} else {
col.sd.bar <- layout$kde$colour$sd.line
}
if(length(layout$kde$colour$background) == 1) {
col.background <- c(layout$kde$colour$background, 2:length(data))
} else {
col.background <- layout$kde$colour$background
}
}
if("lty" %in% names(list(...))) {
lty <- list(...)$lty
} else {
lty <- rep(1, length(data))
}
if("lwd" %in% names(list(...))) {
lwd <- list(...)$lwd
} else {
lwd <- rep(1, length(data))
}
if("cex" %in% names(list(...))) {
cex <- list(...)$cex
} else {
cex <- 1
}
if("fun" %in% names(list(...))) {
fun <- list(...)$fun
} else {
fun <- FALSE
}
## convert keywords into summary placement coordinates
if(missing(summary.pos) == TRUE) {
summary.pos <- c(xlim.plot[1], ylim.plot[2])
summary.adj <- c(0, 1)
} else if(length(summary.pos) == 2) {
summary.pos <- summary.pos
summary.adj <- c(0, 1)
} else if(summary.pos[1] == "topleft") {
summary.pos <- c(xlim.plot[1], ylim.plot[2])
summary.adj <- c(0, 1)
} else if(summary.pos[1] == "top") {
summary.pos <- c(mean(xlim.plot), ylim.plot[2])
summary.adj <- c(0.5, 1)
} else if(summary.pos[1] == "topright") {
summary.pos <- c(xlim.plot[2], ylim.plot[2])
summary.adj <- c(1, 1)
} else if(summary.pos[1] == "left") {
summary.pos <- c(xlim.plot[1], mean(ylim.plot[1:2]))
summary.adj <- c(0, 0.5)
} else if(summary.pos[1] == "center") {
summary.pos <- c(mean(xlim.plot), mean(ylim.plot[1:2]))
summary.adj <- c(0.5, 0.5)
} else if(summary.pos[1] == "right") {
summary.pos <- c(xlim.plot[2], mean(ylim.plot[1:2]))
summary.adj <- c(1, 0.5)
}else if(summary.pos[1] == "bottomleft") {
summary.pos <- c(xlim.plot[1], ylim.plot[1])
summary.adj <- c(0, 0)
} else if(summary.pos[1] == "bottom") {
summary.pos <- c(mean(xlim.plot), ylim.plot[1])
summary.adj <- c(0.5, 0)
} else if(summary.pos[1] == "bottomright") {
summary.pos <- c(xlim.plot[2], ylim.plot[1])
summary.adj <- c(1, 0)
}
## plot data sets -----------------------------------------------------------
## setup plot area
if(length(summary) >= 1 & summary.pos[1] == "sub") {
toplines <- length(data)
} else {
toplines <- 1
}
## extract original plot parameters
par(bg = layout$kde$colour$background)
bg.original <- par()$bg
par(mar = c(5, 5.5, 2.5 + toplines, 4.5),
xpd = FALSE,
cex = cex)
if(layout$kde$dimension$figure.width != "auto" |
layout$kde$dimension$figure.height != "auto") {
par(mai = layout$kde$dimension$margin / 25.4,
pin = c(layout$kde$dimension$figure.width / 25.4 -
layout$kde$dimension$margin[2] / 25.4 -
layout$kde$dimension$margin[4] / 25.4,
layout$kde$dimension$figure.height / 25.4 -
layout$kde$dimension$margin[1] / 25.4 -
layout$kde$dimension$margin[3]/25.4))
}
## create empty plot to get plot dimensions
plot(NA,
xlim = xlim.plot,
ylim = ylim.plot[1:2],
sub = sub,
log = log.option,
axes = FALSE,
ann = FALSE)
## get line height in xy coordinates
l_height <- par()$cxy[2]
## optionally update ylim
if(boxplot == TRUE) {
ylim.plot[1] <- ylim.plot[1] - 1.4 * l_height
}
## create empty plot to set adjusted plot dimensions
par(new = TRUE)
plot(NA,
xlim = xlim.plot,
ylim = ylim.plot[1:2],
log = log.option,
cex = cex,
axes = FALSE,
ann = FALSE)
## add box
box(which = "plot",
col = layout$kde$colour$box)
## add x-axis
axis(side = 1,
col = layout$kde$colour$xtck,
col.axis = layout$kde$colour$xtck,
labels = NA,
tcl = -layout$kde$dimension$xtcl / 200,
cex = cex)
axis(side = 1,
line = 2 * layout$kde$dimension$xtck.line / 100 - 2,
lwd = 0,
col = layout$kde$colour$xtck,
family = layout$kde$font.type$xtck,
font = (1:4)[c("plain", "bold", "italic", "bold italic") ==
layout$kde$font.deco$xtck],
col.axis = layout$kde$colour$xtck,
cex.axis = layout$kde$font.size$xlab/12)
mtext(text = xlab,
side = 1,
line = 3 * layout$kde$dimension$xlab.line / 100,
col = layout$kde$colour$xlab,
family = layout$kde$font.type$xlab,
font = (1:4)[c("plain", "bold", "italic", "bold italic") ==
layout$kde$font.deco$xlab],
cex = cex * layout$kde$font.size$xlab/12)
## add left y-axis
axis(side = 2,
at = pretty(x = range(De.density.range[3:4])),
col = layout$kde$colour$ytck1,
col.axis = layout$kde$colour$ytck1,
labels = NA,
tcl = -layout$kde$dimension$ytck1 / 200,
cex = cex)
axis(side = 2,
at = pretty(x = range(De.density.range[3:4])),
line = 2 * layout$kde$dimension$ytck1.line / 100 - 2,
lwd = 0,
col = layout$kde$colour$ytck1,
family = layout$kde$font.type$ytck1,
font = (1:4)[c("plain", "bold", "italic", "bold italic") ==
layout$kde$font.deco$ytck1],
col.axis = layout$kde$colour$ytck1,
cex.axis = layout$kde$font.size$ylab1/12)
mtext(text = ylab[1],
side = 2,
line = 3 * layout$kde$dimension$ylab1.line / 100,
col = layout$kde$colour$ylab1,
family = layout$kde$font.type$ylab1,
font = (1:4)[c("plain", "bold", "italic", "bold italic") ==
layout$kde$font.deco$ylab1],
cex = cex * layout$kde$font.size$ylab1/12)
for(i in 1:length(data)) {
if(!all(is.na(De.density[[i]]))){
polygon(x = c(par()$usr[1], De.density[[i]]$x, par()$usr[2]),
y = c(min(De.density[[i]]$y),De.density[[i]]$y, min(De.density[[i]]$y)),
border = col.kde.line[i],
col = col.kde.fill[i],
lty = lty[i],
lwd = lwd[i])
}
}
## add plot title
cex.old <- par()$cex
par(cex = layout$kde$font.size$main / 12)
title(main = main,
family = layout$kde$font.type$main,
font = (1:4)[c("plain", "bold", "italic", "bold italic") ==
layout$kde$font.deco$main],
col.main = layout$kde$colour$main,
line = (toplines + 1.2) * layout$kde$dimension$main / 100)
par(cex = cex.old)
## optionally add mtext line
if(mtext != "") {
mtext(text = mtext,
side = 3,
line = 0.5,
family = layout$kde$font.type$mtext,
font = (1:4)[c("plain", "bold", "italic", "bold italic") ==