/
pirateplot_function.R
1932 lines (1680 loc) · 58.1 KB
/
pirateplot_function.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
#' pirateplot
#'
#' The pirateplot function creates an RDI (Raw data, Descriptive and Inferential statistic) plot showing the relationship between 1 to 3 categorical independent variables and 1 continuous dependent variable.
#'
#' @param formula formula. A formula in the form \code{y ~ x1 + x2 + x3} indicating the vertical response variable (y) and up to three independent variables
#' @param data Either a dataframe containing the variables specified in formula, a list of numeric vectors, or a numeric dataframe / matrix.
#' @param plot logical. If \code{TRUE} (the default), thent the pirateplot is produced. If \code{FALSE}, the data summaries created in the plot are returned as a list.
#' @param pal string. The color palette of the plot. Can be a single color, a vector of colors, or the name of a palette in the piratepal() function (e.g.; "basel", "google", "southpark"). To see all the palettes, run \code{piratepal(palette = "all", action = "show")}
#' @param pointpars dataframe A dataframe containing optional additional plotting parameters for points. The dataframe should have thee same number of rows as \code{data}, and have column names in the set \code{col, bg, pch, labels}.
#' @param mix.col,mix.p Optional color mixing arguments to be passed to \code{piratepal}. See \code{?piratepal} for examples.
#' @param point.col,bar.f.col,bean.b.col,bean.f.col,inf.f.col,inf.b.col,avg.line.col,bar.b.col,quant.col,point.bg string. Vectors of colors specifying the colors of the plotting elements. This will override values in the palette. f stands for filling, b stands for border.
#' @param theme integer. An integer in the set 0, 1, 2 specifying a theme (that is, new default values for opacities and colors). \code{theme = 0} turns off all opacities which can then be individually specified individually.
#' @param beside logical. If FALSE, then the second independent variable is spread over separate plots, rather than separated in the samep lot.
#' @param bar.f.o,point.o,inf.f.o,inf.b.o,avg.line.o,bean.b.o,bean.f.o,bar.b.o numeric. A number between 0 and 1 indicating how opaque to make the bars, points, inference band, average line, and beans respectively. These values override whatever is in the specified theme
#' @param avg.line.fun function. A function that determines how average lines and bar heights are determined (default is mean).
#' @param back.col string. Color of the plotting background.
#' @param point.cex,point.pch,point.lwd numeric. The size, pch type, and line width of raw data points.
#' @param bean.lwd,bean.lty,inf.lwd,avg.line.lwd,bar.lwd numeric. Vectors of numbers customizing the look of beans and lines.
#' @param width.min,width.max numeric. The minimum/maximum width of the beans.
#' @param cut.min,cut.max numeric. Optional minimum and maximum values of the beans.
#' @param inf.method string. A string indicating what types of inference bands to calculate. "ci" means frequentist confidence intervals, "hdi" means Bayesian Highest Density Intervals (HDI), "iqr" means interquartile range, "sd" means standard deviation, "se" means standard error, "withinci" means frequentist confidence intervals in a within design (Morey, 2008).
#' @param inf.within string. The variable which serves as an ID variable in a within design.
#' @param inf.disp string. How should inference ranges be displayed? \code{"line"} creates a classic vertical line, \code{"rect"} creates a rectangle, \code{"bean"} forms the inference around the bean.
#' @param inf.p numeric. A number adjusting how inference ranges are calculated. for \code{"ci"} and \code{"hdi"}, a number between 0 and 1 indicating the level of confidence (default is .95). For \code{"sd"} and \code{"se"}, the number of standard deviations / standard errors added to or subtracted from the mean (default is 1).
#' @param hdi.iter integer. Number of iterations to run when calculating the HDI. Larger values lead to better estimates, but can be more time consuming.
#' @param bw,adjust Arguments passed to density calculations for beans (see ?density)
#' @param jitter.val numeric. Amount of jitter added to points horizontally. Defaults to 0.05.
#' @param at integer. Locations of the beans. Especially helpful when adding beans to an existing plot with add = TRUE
#' @param sortx string. How to sort the x values. Can be "sequential" (as they are found in the original dataframe), "alphabetical", or a string in the set ("mean", "median", "min", "max") indicating a function
#' @param decreasing logical. If sortx is a named function, should values be sorted in decreasing order?
#' @param add logical. Should the pirateplot elements be added to an existing plotting space?
#' @param cap.beans logical. Should maximum and minimum values of the bean densities be capped at the limits found in the data? Default is FALSE.
#' @param quant.length,quant.lwd numeric. Specifies line lengths/widths of \code{quant}.
#' @param family a font family (Not currently in use)
#' @param cex.lab,cex.axis,cex.names Size of the labels, axes, and bean names.
#' @param force.layout logical. If TRUE, then all internal plotting formatting functions (e.g.; mfrow, mar) are turned off.
#' @param yaxt.y numeric. Locations of tick marks on the y-axis
#' @param gl.y numeric. Locations of the horizontal grid lines
#' @param gl.lwd,gl.lty,gl.col Customization for grid lines. Can be entered as vectors for alternating gridline types
#' @param bty,xlim,ylim,xlab,ylab,main,yaxt,xaxt General plotting arguments
#' @param quant numeric. Adds horizontal lines representing custom quantiles.
#' @param bar.b.lwd,line.fun,inf.o,bean.o,inf.col,theme.o,inf,inf.type,inf.band,bar.o,line.o,hdi.o,gl depricated arguments
#' @keywords plot
#' @importFrom BayesFactor ttestBF
#' @importFrom grDevices col2rgb gray rgb
#' @importFrom graphics abline axis layout mtext par plot points polygon rasterImage rect segments text axTicks
#' @importFrom stats density model.frame optimize rnorm t.test qbeta sd quantile IQR aggregate as.formula
#' @importFrom utils vignette
#' @export
#' @examples
#'
#'
#' # Default pirateplot of weight by Time
#' pirateplot(
#' formula = weight ~ Time,
#' data = ChickWeight
#' )
#'
#' # Same but in grayscale
#' pirateplot(
#' formula = weight ~ Time,
#' data = ChickWeight,
#' pal = "gray"
#' )
#'
#'
#' # Now using theme 2
#' pirateplot(
#' formula = weight ~ Time,
#' data = ChickWeight,
#' main = "Chicken weight by time",
#' theme = 2
#' ) # theme 2
#'
#' # theme 3
#' pirateplot(
#' formula = weight ~ Time,
#' data = ChickWeight,
#' main = "Chicken weight by time",
#' theme = 3
#' ) # theme 3
#'
#' # theme 4
#' pirateplot(
#' formula = weight ~ Time,
#' data = ChickWeight,
#' main = "Chicken weight by time",
#' theme = 4
#' ) # theme 4
#'
#' # Start with theme 2, but then customise!
#' pirateplot(
#' formula = weight ~ Time,
#' data = ChickWeight,
#' theme = 2, # theme 2
#' pal = "xmen", # xmen palette
#' main = "Chicken weights by Time",
#' point.o = .4, # Add points
#' point.col = "black",
#' point.bg = "white",
#' point.pch = 21,
#' bean.f.o = .2, # Turn down bean filling
#' inf.f.o = .8, # Turn up inf filling
#' gl.col = "gray", # gridlines
#' gl.lwd = c(.5, 0)
#' ) # turn off minor grid lines
#'
#' # 2 IVs
#' pirateplot(
#' formula = len ~ dose + supp,
#' data = ToothGrowth,
#' main = "Guinea pig tooth length by supplement",
#' point.pch = 16, # Point specifications...
#' point.col = "black",
#' point.o = .7,
#' inf.f.o = .9, # inference band opacity
#' gl.col = "gray"
#' )
#'
#'
#' # Build everything from scratch with theme 0
#' # And use 3 IVs
#' pirateplot(
#' formula = height ~ headband + eyepatch + sex,
#' data = pirates,
#' pal = gray(.1), # Dark gray palette
#' theme = 0, # Start from scratch
#' inf.f.o = .7, # Band opacity
#' inf.f.col = piratepal("basel"), # Add color to bands
#' point.o = .1, # Point opacity
#' avg.line.o = .8, # Average line opacity
#' gl.col = gray(.6), # Gridline specifications
#' gl.lty = 1,
#' gl.lwd = c(.5, 0)
#' )
#'
#' # See the vignette for more details
#' vignette("pirateplot", package = "yarrr")
#'
pirateplot <- function(
formula = NULL,
data = NULL,
plot = TRUE,
avg.line.fun = mean,
pal = "basel",
pointpars = NULL,
mix.col = "white",
mix.p = 0,
back.col = NULL,
point.cex = NULL,
point.pch = NULL,
point.lwd = 1,
jitter.val = .03,
theme = 1,
beside = TRUE,
bean.b.o = NULL,
bean.f.o = NULL,
point.o = NULL,
bar.f.o = NULL,
bar.b.o = NULL,
inf.f.o = NULL,
inf.b.o = NULL,
avg.line.o = NULL,
gl.col = NULL,
point.col = NULL,
point.bg = NULL,
bar.f.col = NULL,
bean.b.col = NULL,
bean.f.col = NULL,
inf.f.col = NULL,
inf.b.col = NULL,
avg.line.col = NULL,
bar.b.col = NULL,
quant.col = NULL,
avg.line.lwd = 4,
bean.lwd = 1,
bean.lty = 1,
inf.lwd = NULL,
bar.lwd = 1,
at = NULL,
bw = "nrd0",
adjust = 1,
add = FALSE,
sortx = "alphabetical",
decreasing = FALSE,
cex.lab = 1,
cex.axis = 1,
cex.names = 1,
force.layout = FALSE,
quant = NULL,
quant.length = NULL,
quant.lwd = NULL,
bty = "o",
cap.beans = TRUE,
family = NULL,
inf.method = "hdi",
inf.within = NULL,
inf.p = NULL,
hdi.iter = 1e3,
inf.disp = NULL,
cut.min = NULL,
cut.max = NULL,
width.min = .3,
width.max = NA,
ylim = NULL,
xlim = NULL,
xlab = NULL,
ylab = NULL,
main = NULL,
yaxt = NULL,
xaxt = NULL,
yaxt.y = NULL,
gl.y = NULL,
gl.lwd = NULL,
gl.lty = NULL,
bar.b.lwd = NULL,
line.fun = NULL,
line.o = NULL,
inf.o = NULL,
bean.o = NULL,
inf.col = NULL,
theme.o = NULL,
bar.o = NULL,
inf = NULL,
hdi.o = NULL,
inf.type = NULL,
inf.band = NULL,
gl = NULL) {
#
#
#
# # #
# formula = len ~ dose + supp
# data = ToothGrowth
# plot = TRUE
# avg.line.fun = mean
# pal = "basel"
# back.col = NULL
# point.cex = NULL
# point.pch = NULL
# point.lwd = 1
# jitter.val = .03
# theme = 1
# beside = TRUE
# bean.b.o = NULL
# bean.f.o = NULL
# point.o = NULL
# bar.f.o = NULL
# bar.b.o = NULL
# inf.f.o = NULL
# inf.b.o = NULL
# force.layout = FALSE
# avg.line.o = NULL
# gl.col = NULL
# point.col = NULL
# point.bg = NULL
# bar.f.col = NULL
# bean.b.col = NULL
# bean.f.col = NULL
# inf.f.col = NULL
# inf.b.col = NULL
# avg.line.col = NULL
# bar.b.col = NULL
# quant.col = NULL
# avg.line.lwd = 4
# bean.lwd = 1
# bean.lty = 1
# inf.lwd = NULL
# bar.lwd = 1
# at = NULL
# bw = "nrd0"
# adjust = 1
# add = FALSE
# sortx = "sequential"
# decreasing = TRUE
# cex.lab = 1
# cex.axis = 1
# cex.names = 1
# quant = NULL
# quant.length = NULL
# quant.lwd = NULL
# bty = "o"
# mix.p <- 0
# evidence = FALSE
# family = NULL
# inf.method = "hdi"
# inf.p = .95
# hdi.iter = 1e3
# inf.disp = "line"
# cut.min = NULL
# cut.max = NULL
# width.min = .3
# width.max = NA
# ylim = NULL
# xlim = NULL
# xlab = NULL
# ylab = NULL
# main = NULL
# yaxt = NULL
# xaxt = NULL
# gl.lwd = NULL
# gl.lty = NULL
# bar.b.lwd = NULL
# line.fun = NULL
# inf.o = NULL
# bean.o = NULL
# bar.o = NULL
# line.o = NULL
# inf.col = NULL
# theme.o = NULL
# inf = NULL
# inf.type = NULL
# inf.band = NULL
# cap.beans = TRUE
# yaxt.y <- NULL
# gl.y <- NULL
# pointpars = NULL
# #
#
#
# -----
# SETUP
# ------
{
# Check for depricated arguments
{
if (is.null(bar.b.lwd) == FALSE) {
message("bar.b.lwd is depricated. Use bar.lwd instead")
bar.lwd <- bar.b.lwd
}
if (is.null(line.fun) == FALSE) {
message("line.fun is depricated. Use avg.line.fun instead")
avg.line.fun <- line.fun
}
if (is.null(inf.o) == FALSE) {
message("inf.o is depricated. Use inf.f.o instead")
inf.f.o <- inf.o
}
if (is.null(line.o) == FALSE) {
message("line.o is depricated. Use avg.line.o instead")
avg.line.o <- line.o
}
if (is.null(bean.o) == FALSE) {
message("bean.o is depricated. Use bean.b.o instead")
bean.b.o <- bean.o
}
if (is.null(inf.col) == FALSE) {
message("inf.col is depricated. Use inf.f.col instead")
inf.f.col <- inf.col
}
if (is.null(theme.o) == FALSE) {
message("theme.o is depricated. Use theme instead")
theme <- theme.o
}
if (is.null(inf) == FALSE) {
message("inf is depricated. Use inf.method instead")
inf.method <- inf
}
if (is.null(inf.band) == FALSE) {
message("inf.band is depricated. Use inf.disp instead")
inf.disp <- inf.band
}
if (is.null(bar.o) == FALSE) {
message("bar.o is depricated. Use bar.f.o (for filling), and bar.b.o (for border) instead")
bar.f.o <- bar.o
}
}
# Look for missing critical inputs
{
if (is.null(data)) {
stop("You must specify data in the data argument")
}
}
# Set some defaults
if (inf.method %in% c("hdi", "ci", "withinci") & is.null(inf.p)) {
inf.p <- .95
}
if (inf.method %in% c("sd", "se") & is.null(inf.p)) {
inf.p <- 1
}
# If no formula, than reshape data
if (is.null(formula)) {
if (inherits(data, c("data.frame", "matrix"))) {
# data <- data.frame(a = rnorm(100), b = rnorm(100), c = rnorm(100))
# data <- matrix(rnorm(100), nrow = 20, ncol = 5)
#
data <- as.data.frame(data)
iv.levels <- names(data)
data <- stats::reshape(data, direction = "long", varying = list(1:ncol(data)))
data <- data[, 1:2]
names(data) <- c("group", "y")
group.num <- data$group
for (i in 1:length(iv.levels)) {
data$group[group.num == i] <- iv.levels[i]
}
formula <- y ~ group
}
if (inherits(data, c("numeric", "integer"))) {
data <- data.frame(y = data, group = rep(1, length(data)))
formula <- y ~ group
xlab <- ""
if (is.null(xaxt)) {
xaxt <- "n"
}
if (is.null(ylab)) {
ylab <- ""
}
}
if (inherits(data, "list")) {
if (is.null(names(data))) {
names(data) <- paste0("V", 1:length(data))
}
iv.levels <- names(data)
# Convert list to dataframe
data.df <- do.call("rbind", lapply(1:length(data), FUN = function(x) {
data.frame(
group = rep(iv.levels[x], length(data[[x]])),
y = unlist(data[[x]])
)
}))
data <- data.df
formula <- y ~ group
}
}
# Reshape dataframe to include relevant variables
{
withindata <- data
data <- model.frame(
formula = formula,
data = data
)
dv.name <- names(data)[1]
dv.v <- data[, 1]
all.iv.names <- names(data)[2:ncol(data)]
}
# GET IV INFORMATION
{
n.iv <- ncol(data) - 1
if (n.iv > 3) {
stop("Currently only 1, 2, or 3 IVs are supported in pirateplot(). Please reduce.")
}
# selection.mtx dictates which values are in each sub-plot
if (n.iv == 1 | (n.iv == 2 & beside == TRUE)) {
selection.mtx <- matrix(TRUE, nrow = nrow(data), ncol = 1)
}
if (n.iv == 2 & beside == FALSE) {
iv2.name <- names(data)[3]
iv2.levels <- sort(unique(data[, 3]))
selection.mtx <- matrix(unlist(lapply(iv2.levels, FUN = function(x) {
data[, 3] == x
})), nrow = nrow(data), ncol = length(iv2.levels), byrow = FALSE)
}
if (n.iv == 3) {
iv3.name <- names(data)[4]
iv3.levels <- sort(unique(data[, 4]))
selection.mtx <- matrix(unlist(lapply(iv3.levels, FUN = function(x) {
data[, 4] == x
})), nrow = nrow(data), ncol = length(iv3.levels), byrow = F)
}
n.subplots <- ncol(selection.mtx)
# Loop over subplots (only relevant when there is a third IV)
if (force.layout == FALSE) {
if (n.subplots == 2) {
par(mfrow = c(1, 2))
}
if (n.subplots == 3) {
par(mfrow = c(1, 3))
}
if (n.subplots == 4) {
par(mfrow = c(2, 2))
}
if (n.subplots %in% c(5, 6)) {
par(mfrow = c(2, 3))
}
if (n.subplots > 7) {
par(mfrow = c(ceiling(sqrt(n.subplots)), ceiling(sqrt(n.subplots))))
}
}
}
# Setup outputs
summary.ls <- vector("list", length = n.subplots)
}
# Loop over subplots
for (subplot.i in 1:n.subplots) {
# Select data for current subplot
data.i <- data[selection.mtx[, subplot.i], ]
if (is.null(pointpars) == FALSE) {
pointpars.i <- pointpars[selection.mtx[, subplot.i], ]
}
# Remove potential iv3 column
data.i <- data.i[, 1:min(ncol(data.i), 3)]
# Determine levels of each IV
{
if ((substr(sortx, 1, 1) %in% c("a", "s") |
sortx %in% c("mean", "median", "min", "max")) == FALSE
) {
stop("sortx argument is invalid. use 'alphabetical', 'sequential', 'mean', 'median', 'min', or 'max'")
}
if (substr(sortx, 1, 1) == "a") {
iv.levels <- lapply(2:ncol(data.i),
FUN = function(x) {
sort(unique(data.i[, x]))
}
)
}
if (substr(sortx, 1, 1) == "s") {
iv.levels <- lapply(2:ncol(data.i), FUN = function(x) {
unique(data.i[, x])
})
}
if (sortx %in% c("mean", "median", "min", "max")) {
agg <- aggregate(formula, data = data.i, FUN = get(sortx))
if (ncol(agg) == 2) {
agg <- agg[order(agg[, 2], decreasing = decreasing), ]
iv.levels <- list(paste(agg[, 1]))
}
if (ncol(agg) == 3) {
agg.1 <- aggregate(as.formula(paste(dv.name, "~", all.iv.names[1])),
data = data.i, FUN = get(sortx)
)
agg.1 <- agg.1[order(agg.1[, 2], decreasing = decreasing), ]
iv.levels <- list(paste(agg.1[, 1]))
agg.2 <- aggregate(as.formula(paste(dv.name, "~", all.iv.names[2])),
data = data.i, FUN = get(sortx)
)
agg.2 <- agg.2[order(agg.2[, 2], decreasing = decreasing), ]
iv.levels[2] <- list(paste(agg.2[, 1]))
}
}
iv.lengths <- sapply(1:length(iv.levels), FUN = function(x) {
length(iv.levels[[x]])
})
iv.names <- names(data.i)[2:ncol(data.i)]
subplot.n.iv <- length(iv.levels)
}
# Set up bean info
{
bean.mtx <- expand.grid(iv.levels)
names(bean.mtx) <- names(data.i)[2:ncol(data.i)]
n.beans <- nrow(bean.mtx)
bean.mtx$bean.num <- 1:nrow(bean.mtx)
# Determine bean x locations
if (is.null(at)) {
bean.loc <- 1:n.beans
group.spacing <- 1
if (subplot.n.iv == 2) {
bean.loc <- bean.loc + rep(group.spacing * (0:(iv.lengths[2] - 1)),
each = iv.lengths[1]
)
}
}
if (!is.null(at)) {
bean.loc <- rep(at, length.out = n.beans)
}
bean.mtx$x.loc <- bean.loc
data.i <- merge(data.i, bean.mtx)
}
# Calculate summary statistics for each bean
{
summary <- data.frame(
"n" = rep(NA, n.beans),
"avg" = rep(NA, n.beans),
"inf.lb" = rep(NA, n.beans),
"inf.ub" = rep(NA, n.beans)
)
summary <- cbind(bean.mtx[, (1:ncol(bean.mtx) - 1)], summary)
if (n.subplots > 1 & beside == TRUE) {
summary[iv3.name] <- iv3.levels[subplot.i]
}
# Loop over beans in subplot
for (bean.i in 1:n.beans) {
dv.i <- data.i[data.i$bean.num == bean.i, dv.name]
if (is.logical(dv.i)) {
dv.i <- as.numeric(dv.i)
}
summary$n[bean.i] <- length(dv.i)
summary$avg[bean.i] <- avg.line.fun(dv.i)
# Calculate inference
if (length(dv.i) > 0) {
# Binary data.i
if (length(setdiff(dv.i, c(0, 1))) == 0) {
if (inf.method == "hdi") {
# Calculate HDI from beta(Success + 1, Failure + 1)
inf.lb <- qbeta(.025, shape1 = sum(dv.i) + 1, shape2 = sum(dv.i == 0) + 1)
inf.ub <- qbeta(.975, shape1 = sum(dv.i) + 1, shape2 = sum(dv.i == 0) + 1)
}
if (inf.method == "ci") {
# Calculate 95% CI with Normal distribution approximation to binomial
inf.lb <- mean(dv.i) - 1.96 * sqrt(mean(dv.i) * (1 - mean(dv.i)) / length(dv.i)) - .5 / length(dv.i)
inf.ub <- mean(dv.i) + 1.96 * sqrt(mean(dv.i) * (1 - mean(dv.i)) / length(dv.i)) + .5 / length(dv.i)
if (inf.lb < 0) {
inf.lb <- 0
}
if (inf.lb > 1) {
inf.ub <- 1
}
}
if (inf.method == "sd") {
inf.lb <- mean(dv.i) - sd(dv.i) * inf.p
inf.ub <- mean(dv.i) + sd(dv.i) * inf.p
if (inf.lb < 0) {
inf.lb <- 0
}
if (inf.lb > 1) {
inf.ub <- 1
}
}
if (inf.method == "se") {
inf.lb <- mean(dv.i) - sd(dv.i) / sqrt(length(dv.i)) * inf.p
inf.ub <- mean(dv.i) + sd(dv.i) / sqrt(length(dv.i)) * inf.p
if (inf.lb < 0) {
inf.lb <- 0
}
if (inf.lb > 1) {
inf.ub <- 1
}
}
}
# Non-Binary data.i
if (length(setdiff(dv.i, c(0, 1))) > 0) {
if (inf.method == "hdi") {
ttest.bf <- BayesFactor::ttestBF(dv.i, posterior = T, iterations = hdi.iter, progress = F)
samples <- ttest.bf[, 1]
# using the hdi function from Kruschke
inf.lb <- hdi(samples, credMass = inf.p)[1]
inf.ub <- hdi(samples, credMass = inf.p)[2]
}
if (inf.method == "iqr") {
inf.lb <- quantile(dv.i, probs = .25)
inf.ub <- quantile(dv.i, probs = .75)
}
if (inf.method == "ci") {
ci.i <- t.test(dv.i, conf.level = inf.p)$conf.int
inf.lb <- ci.i[1]
inf.ub <- ci.i[2]
}
if (inf.method == "sd") {
inf.lb <- mean(dv.i) - sd(dv.i) * inf.p
inf.ub <- mean(dv.i) + sd(dv.i) * inf.p
}
if (inf.method == "se") {
inf.lb <- mean(dv.i) - sd(dv.i) / sqrt(length(dv.i)) * inf.p
inf.ub <- mean(dv.i) + sd(dv.i) / sqrt(length(dv.i)) * inf.p
}
if (inf.method == "withinci") {
grandMean <- mean(dv.v)
Groups <- unique(withindata[inf.within])[[1]]
groupMean <- c()
dv.within <- c()
for (group in 1:length(Groups)) {
# get the participant/group mean for each participant/group
groupMean[group] <- mean(dv.v[which(withindata[inf.within] == Groups[group])])
}
for (datum in 1:length(dv.i)) {
# substitute group mean with grand mean, which removes between subject variance
dv.within[datum] <- dv.i[datum] - groupMean[datum] + grandMean
}
ci.i <- t.test(dv.within, conf.level = inf.p)$conf.int
ci.width <- (ci.i[2] - ci.i[1])
inf.lb <- mean(dv.i) - (ci.width / 2) * sqrt(n.beans / (n.beans - 1)) # with Morey correction
inf.ub <- mean(dv.i) + (ci.width / 2) * sqrt(n.beans / (n.beans - 1)) # with Morey correction
}
}
summary$inf.lb[bean.i] <- inf.lb
summary$inf.ub[bean.i] <- inf.ub
}
if (length(dv.i) == 0) {
summary$inf.lb[bean.i] <- NA
summary$inf.ub[bean.i] <- NA
}
}
}
if (plot == TRUE) {
# COLORS AND TRANSPARENCIES
{
# Set number of colors to number of levels of the first IV
n.cols <- iv.lengths[1]
# DEFINE THEMES
{
if ((theme %in% 0:4) == FALSE) {
print("theme must be an integer between 0 and 4. I'll set it to 1 for now.")
theme <- 1
}
if (theme == 0) {
if (is.null(point.pch)) {
point.pch <- 16
}
if (is.null(point.o)) {
point.o <- 0
}
if (is.null(bean.b.o)) {
bean.b.o <- 0
}
if (is.null(bean.f.o)) {
bean.f.o <- 0
}
if (is.null(inf.f.o)) {
inf.f.o <- 0
}
if (is.null(inf.b.o)) {
inf.b.o <- 0
}
if (is.null(avg.line.o)) {
avg.line.o <- 0
}
if (is.null(bar.f.o)) {
bar.f.o <- 0
}
if (is.null(bar.b.o)) {
bar.b.o <- 0
}
if (is.null(point.cex)) {
point.cex <- 1
}
if (is.null(gl.col)) {
gl.col <- "white"
}
if (is.null(inf.disp)) {
inf.disp <- "rect"
}
}
if (theme == 1) {
if (is.null(point.o)) {
point.o <- .2
}
if (is.null(bean.b.o)) {
bean.b.o <- 1
}
if (is.null(bean.f.o)) {
bean.f.o <- .3
}
if (is.null(inf.f.o)) {
inf.f.o <- .8
}
if (is.null(inf.b.o)) {
inf.b.o <- .8
}
if (is.null(avg.line.o)) {
avg.line.o <- 1
}
if (is.null(bar.f.o)) {
bar.f.o <- 0
}
if (is.null(bar.b.o)) {
bar.b.o <- 0
}
if (is.null(bean.b.col)) {
bean.b.col <- "black"
}
if (is.null(point.cex)) {
point.cex <- .7
}
if (is.null(point.col)) {
point.col <- "black"
}
if (is.null(inf.b.col)) {
inf.b.col <- gray(0)
}
if (is.null(inf.f.col)) {
inf.f.col <- "white"
}
if (is.null(bean.lwd)) {
bean.lwd <- 2
}
if (is.null(avg.line.col)) {
avg.line.col <- "black"
}
if (is.null(gl.col)) {
gl.col <- "gray"
}
if (is.null(gl.lwd)) {
gl.lwd <- c(.25, .5)
}
if (is.null(point.col)) {
point.col <- "black"
}
if (is.null(point.bg)) {
point.bg <- "white"
}
if (is.null(point.pch)) {
point.pch <- 16
}
if (is.null(inf.disp)) {
inf.disp <- "rect"
}
}
if (theme == 2) {
if (is.null(point.pch)) {
point.pch <- 16
}
if (is.null(point.o)) {
point.o <- .1
}
if (is.null(bean.b.o)) {
bean.b.o <- 1
}
if (is.null(bean.f.o)) {
bean.f.o <- 1
}
if (is.null(inf.f.o)) {
inf.f.o <- .6
}
if (is.null(inf.b.o)) {
inf.b.o <- .8
}
if (is.null(inf.b.col)) {
inf.b.col <- gray(0)
}
if (is.null(avg.line.o)) {
avg.line.o <- 1
}
if (is.null(bar.f.o)) {
bar.f.o <- 0
}
if (is.null(bar.b.o)) {
bar.b.o <- 0
}
if (is.null(bean.b.col)) {
bean.b.col <- "black"
}
if (is.null(point.cex)) {
point.cex <- .7
}
if (is.null(point.col)) {
point.col <- "black"
}
if (is.null(bean.lwd)) {
bean.lwd <- 2
}
if (is.null(avg.line.col)) {
avg.line.col <- "black"
}
if (is.null(bean.f.col)) {
bean.f.col <- "white"
}
if (is.null(inf.disp)) {
inf.disp <- "rect"
}
if (is.null(gl.col)) {
gl.col <- "gray"
}
if (is.null(gl.lwd)) {
gl.lwd <- c(.25, .5)
}
}
if (theme == 3) {
if (is.null(point.pch)) {
point.pch <- 16
}
if (is.null(point.o)) {
point.o <- .3
}
if (is.null(bean.b.o)) {
bean.b.o <- 1
}
if (is.null(bean.f.o)) {
bean.f.o <- .5
}
if (is.null(inf.f.o)) {
inf.f.o <- .9
}
if (is.null(inf.b.o)) {
inf.b.o <- 1
}
if (is.null(avg.line.o)) {
avg.line.o <- 1
}
if (is.null(bar.f.o)) {
bar.f.o <- 0
}
if (is.null(bar.b.o)) {
bar.b.o <- 0
}
if (is.null(bean.b.col)) {
bean.b.col <- "black"
}
if (is.null(inf.f.col)) {