77# ' columns have an equal relative width.
88# ' @param heights relative height of each row on a 0-1 scale. By default all
99# ' rows have an equal relative height.
10- # ' @param share determines whether x/y/both axes are shared.
11- # ' @param which_layout adopt the layout of which plot? If the default value of
12- # ' "merge" is used, all plot level layout options will be included in the final
13- # ' layout. This argument also accepts a numeric vector which will restric
10+ # ' @param shareX should the x-axis be shared amongst the subplots?
11+ # ' @param shareY should the y-axis be shared amongst the subplots?
1412# ' @param margin either a single value or four values (all between 0 and 1).
1513# ' If four values are provided, the first is used as the left margin, the second
1614# ' is used as the right margin, the third is used as the top margin, and the
1715# ' fourth is used as the bottom margin.
1816# ' If a single value is provided, it will be used as all four margins.
17+ # ' @param which_layout adopt the layout of which plot? If the default value of
18+ # ' "merge" is used, all plot level layout options will be included in the final
19+ # ' layout. This argument also accepts a numeric vector specifying
1920# ' @return A plotly object
2021# ' @export
2122# ' @author Carson Sievert
2526# ' subplot(p1, p2, p1, p2, nrows = 2)
2627# ' }
2728
28- subplot <- function (... , nrows = 1 , widths = NULL , heights = NULL , share = NULL ,
29- which_layout = " merge " , margin = 0.02 ) {
29+ subplot <- function (... , nrows = 1 , widths = NULL , heights = NULL , shareX = FALSE ,
30+ shareY = FALSE , margin = 0.02 , which_layout = " merge " ) {
3031 # build each plot and collect relevant info
3132 plots <- lapply(list (... ), plotly_build )
3233 traces <- lapply(plots , " [[" , " data" )
@@ -51,13 +52,24 @@ subplot <- function(..., nrows = 1, widths = NULL, heights = NULL, share = NULL,
5152 xAxisN <- vapply(xAxes , length , numeric (1 ))
5253 yAxisN <- vapply(yAxes , length , numeric (1 ))
5354 # old -> new axis name dictionary
55+ ncols <- ceiling(length(plots ) / nrows )
56+ xAxisID <- if (shareX ) {
57+ rep(rep(1 : ncols , length.out = length(plots )), xAxisN )
58+ } else {
59+ seq_len(sum(xAxisN ))
60+ }
61+ yAxisID <- if (shareY ) {
62+ rep(rep(1 : nrows , each = ncols , length.out = length(plots )), yAxisN )
63+ } else {
64+ seq_len(sum(yAxisN ))
65+ }
5466 xAxisMap <- setNames(
5567 unlist(lapply(xAxes , names )),
56- paste0(" xaxis" , sub(" ^1$" , " " , seq_len(sum( xAxisN )) ))
68+ paste0(" xaxis" , sub(" ^1$" , " " , xAxisID ))
5769 )
5870 yAxisMap <- setNames(
5971 unlist(lapply(yAxes , names )),
60- paste0(" yaxis" , sub(" ^1$" , " " , seq_len(sum( yAxisN )) ))
72+ paste0(" yaxis" , sub(" ^1$" , " " , yAxisID ))
6173 )
6274 # split the map by plot ID
6375 xAxisMap <- split(xAxisMap , rep(seq_along(plots ), xAxisN ))
@@ -109,7 +121,7 @@ subplot <- function(..., nrows = 1, widths = NULL, heights = NULL, share = NULL,
109121 # start merging the plots into a single subplot
110122 p <- list (
111123 data = Reduce(c , traces ),
112- layout = Reduce(c , c(xAxes , yAxes ))
124+ layout = Reduce(modifyList , c(xAxes , rev( yAxes ) ))
113125 )
114126 p $ layout $ annotations <- Reduce(c , annotations )
115127 p $ layout $ shapes <- Reduce(c , shapes )
0 commit comments