1414# ' is used as the right margin, the third is used as the top margin, and the
1515# ' fourth is used as the bottom margin.
1616# ' If a single value is provided, it will be used as all four margins.
17+ # ' @param keep_titles should axis titles be retained?
1718# ' @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
19+ # ' "merge" is used, layout options found later in the sequence of plots will
20+ # ' override options found earlier in the sequence. This argument also accepts a
21+ # ' numeric vector specifying which plots to consider when merging.
2022# ' @return A plotly object
2123# ' @export
2224# ' @author Carson Sievert
2729# ' }
2830
2931subplot <- function (... , nrows = 1 , widths = NULL , heights = NULL , shareX = FALSE ,
30- shareY = FALSE , margin = 0.02 , which_layout = " merge" ) {
32+ shareY = FALSE , margin = 0.02 , which_layout = " merge" ,
33+ keep_titles = FALSE ) {
3134 # build each plot and collect relevant info
3235 plots <- lapply(list (... ), plotly_build )
3336 traces <- lapply(plots , " [[" , " data" )
@@ -46,8 +49,10 @@ subplot <- function(..., nrows = 1, widths = NULL, heights = NULL, shareX = FALS
4649 x [grepl(" ^yaxis" , names(x ))] %|| % list (yaxis = list (domain = c(0 , 1 ), anchor = " x" ))
4750 })
4851 # remove their titles
49- xAxes <- lapply(xAxes , function (x ) lapply(x , function (y ) { y $ title <- NULL ; y }))
50- yAxes <- lapply(yAxes , function (x ) lapply(x , function (y ) { y $ title <- NULL ; y }))
52+ if (! keep_titles ) {
53+ xAxes <- lapply(xAxes , function (x ) lapply(x , function (y ) { y $ title <- NULL ; y }))
54+ yAxes <- lapply(yAxes , function (x ) lapply(x , function (y ) { y $ title <- NULL ; y }))
55+ }
5156 # number of x/y axes per plot
5257 xAxisN <- vapply(xAxes , length , numeric (1 ))
5358 yAxisN <- vapply(yAxes , length , numeric (1 ))
@@ -75,7 +80,6 @@ subplot <- function(..., nrows = 1, widths = NULL, heights = NULL, shareX = FALS
7580 xAxisMap <- split(xAxisMap , rep(seq_along(plots ), xAxisN ))
7681 yAxisMap <- split(yAxisMap , rep(seq_along(plots ), yAxisN ))
7782 # domains of each subplot
78- # TODO: allow control of column width and row height!
7983 domainInfo <- get_domains(
8084 length(plots ), nrows , margin , widths = widths , heights = heights
8185 )
@@ -137,7 +141,6 @@ subplot <- function(..., nrows = 1, widths = NULL, heights = NULL, shareX = FALS
137141 layouts <- layouts [which_layout ]
138142 }
139143 p $ layout <- c(p $ layout , Reduce(modifyList , layouts ))
140-
141144 hash_plot(data.frame (), p )
142145}
143146
0 commit comments