45
45
# ' @param main.yanchor Character: "top", "middle", "bottom"
46
46
# ' @param subtitle.x Numeric: X position of subtitle relative to paper
47
47
# ' @param subtitle.y Numeric: Y position of subtitle relative to paper
48
+ # ' @param subtitle.xref Character: "paper", "x", "y"
49
+ # ' @param subtitle.yref Character: "paper", "x", "y"
48
50
# ' @param subtitle.xanchor Character: "left", "center", "right"
49
51
# ' @param subtitle.yanchor Character: "top", "middle", "bottom"
50
52
# ' @param scrollZoom Logical: If TRUE, enable scroll zoom
53
55
# ' @param symbol Character: Marker symbol.
54
56
# ' @param scatter.type Character: "scatter", "scattergl", "scatter3d", "scatterternary",
55
57
# ' "scatterpolar", "scattermapbox",
58
+ # ' @param show.marginal.x Logical: If TRUE, add marginal distribution line markers on x-axis
59
+ # ' @param show.marginal.y Logical: If TRUE, add marginal distribution line markers on y-axis
60
+ # ' @param marginal.x Numeric: Data whose distribution will be shown on x-axis. Only
61
+ # ' specify if different from `x`
62
+ # ' @param marginal.y Numeric: Data whose distribution will be shown on y-axis. Only
63
+ # ' specify if different from `y`
64
+ # ' @param marginal.x.y Numeric: Y position of marginal markers on x-axis
65
+ # ' @param marginal.col Color for marginal markers
66
+ # ' @param marginal.alpha Numeric: Alpha for marginal markers
67
+ # ' @param marginal.size Numeric: Size of marginal markers
56
68
# '
57
69
# ' @author E.D. Gennatas
58
70
# ' @export
@@ -98,6 +110,16 @@ dplot3_xy <- function(x, y = NULL,
98
110
se.col = NULL ,
99
111
se.alpha = .4 ,
100
112
scatter.type = " scatter" ,
113
+ # Marginal plots
114
+ show.marginal.x = FALSE ,
115
+ show.marginal.y = FALSE ,
116
+ marginal.x = x ,
117
+ marginal.y = y ,
118
+ marginal.x.y = NULL ,
119
+ marginal.y.x = NULL ,
120
+ marginal.col = NULL ,
121
+ marginal.alpha = .333 ,
122
+ marginal.size = 5 ,
101
123
legend = NULL ,
102
124
legend.xy = c(0 , .98 ),
103
125
legend.xanchor = " left" ,
@@ -120,6 +142,8 @@ dplot3_xy <- function(x, y = NULL,
120
142
main.yanchor = " bottom" ,
121
143
subtitle.x = 0.02 ,
122
144
subtitle.y = 0.99 ,
145
+ subtitle.xref = " paper" ,
146
+ subtitle.yref = " paper" ,
123
147
subtitle.xanchor = " left" ,
124
148
subtitle.yanchor = " top" ,
125
149
automargin.x = TRUE ,
@@ -267,6 +291,10 @@ dplot3_xy <- function(x, y = NULL,
267
291
}
268
292
}
269
293
294
+ # Marginal data ----
295
+ if (show.marginal.x && is.null(marginal.x )) marginal.x <- x
296
+ if (show.marginal.y && is.null(marginal.y )) marginal.y <- y
297
+
270
298
# Reorder ----
271
299
if (order.on.x ) {
272
300
index <- lapply(x , order )
@@ -496,8 +524,57 @@ dplot3_xy <- function(x, y = NULL,
496
524
legendgroup = .names [i ],
497
525
showlegend = legend
498
526
)
527
+ # Marginal plots ----
528
+ # Add marginal plots by plotting short vertical markers on the x and y axes
529
+ if (show.marginal.x ) {
530
+ if (is.null(marginal.col )) {
531
+ marginal.col <- plotly :: toRGB(marker.col , alpha = marginal.alpha )
532
+ }
533
+ if (is.null(marginal.x.y )) marginal.x.y <- ylim [1 ]
534
+ for (i in seq_len(n.groups )) {
535
+ plt <- plotly :: add_trace(plt ,
536
+ x = marginal.x [[i ]],
537
+ y = rep(marginal.x.y , length(marginal.x [[i ]])),
538
+ type = " scatter" ,
539
+ mode = " markers" ,
540
+ marker = list (
541
+ color = marginal.col [[i ]],
542
+ size = marginal.size ,
543
+ symbol = " line-ns-open"
544
+ ),
545
+ showlegend = FALSE ,
546
+ hoverinfo = " x"
547
+ # legendgroup = .names[i],
548
+ # inherit = FALSE
549
+ )
550
+ }
551
+ } # /show.marginal.x
552
+
553
+ if (show.marginal.y ) {
554
+ if (is.null(marginal.col )) {
555
+ marginal.col <- plotly :: toRGB(marker.col , alpha = marginal.alpha )
556
+ }
557
+ if (is.null(marginal.y.x )) marginal.y.x <- xlim [1 ]
558
+ for (i in seq_len(n.groups )) {
559
+ plt <- plotly :: add_trace(plt ,
560
+ x = rep(marginal.y.x , length(marginal.y [[i ]])),
561
+ y = marginal.y [[i ]],
562
+ type = " scatter" ,
563
+ mode = " markers" ,
564
+ marker = list (
565
+ color = marginal.col [[i ]],
566
+ size = marginal.size ,
567
+ symbol = " line-ew-open"
568
+ ),
569
+ showlegend = FALSE ,
570
+ hoverinfo = " y"
571
+ # legendgroup = .names[i]
572
+ )
573
+ }
574
+ } # /show.marginal.y
575
+
576
+ # # { SE band } ----
499
577
if (se.fit ) {
500
- # # { SE band } ----
501
578
plt <- plotly :: add_trace(plt ,
502
579
x = x [[i ]],
503
580
y = fitted [[i ]] + se.times * se [[i ]],
@@ -678,8 +755,8 @@ dplot3_xy <- function(x, y = NULL,
678
755
plt <- plt | > plotly :: add_annotations(
679
756
x = subtitle.x ,
680
757
y = subtitle.y ,
681
- xref = " paper " ,
682
- yref = " paper " ,
758
+ xref = subtitle.xref ,
759
+ yref = subtitle.yref ,
683
760
xanchor = subtitle.xanchor ,
684
761
yanchor = subtitle.yanchor ,
685
762
text = subtitle ,
0 commit comments