Skip to content

Commit dee87c2

Browse files
committed
Remove keep_titles in favor of titleX/titleY
1 parent 79650d7 commit dee87c2

File tree

4 files changed

+30
-24
lines changed

4 files changed

+30
-24
lines changed

R/subplots.R

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
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 shareX should the x-axis be shared amongst the subplots?
11-
#' @param shareY should the y-axis be shared amongst the subplots?
1210
#' @param margin either a single value or four values (all between 0 and 1).
1311
#' If four values are provided, the first is used as the left margin, the second
1412
#' is used as the right margin, the third is used as the top margin, and the
1513
#' fourth is used as the bottom margin.
1614
#' If a single value is provided, it will be used as all four margins.
17-
#' @param keep_titles should axis titles be retained?
15+
#' @param shareX should the x-axis be shared amongst the subplots?
16+
#' @param shareY should the y-axis be shared amongst the subplots?
17+
#' @param titleX should x-axis titles be retained?
18+
#' @param titleY should y-axis titles be retained?
1819
#' @param which_layout adopt the layout of which plot? If the default value of
1920
#' "merge" is used, layout options found later in the sequence of plots will
2021
#' override options found earlier in the sequence. This argument also accepts a
@@ -28,9 +29,9 @@
2829
#' subplot(p1, p2, p1, p2, nrows = 2)
2930
#' }
3031

31-
subplot <- function(..., nrows = 1, widths = NULL, heights = NULL, shareX = FALSE,
32-
shareY = FALSE, margin = 0.02, which_layout = "merge",
33-
keep_titles = FALSE) {
32+
subplot <- function(..., nrows = 1, widths = NULL, heights = NULL, margin = 0.02,
33+
shareX = FALSE, shareY = FALSE, titleX = shareX,
34+
titleY = shareY, which_layout = "merge") {
3435
# build each plot
3536
plotz <- lapply(list(...), plotly_build)
3637
# ensure "axis-reference" trace attributes are properly formatted
@@ -98,9 +99,10 @@ subplot <- function(..., nrows = 1, widths = NULL, heights = NULL, shareX = FALS
9899
yAxes <- lapply(layouts, function(lay) {
99100
lay[grepl("^yaxis|^geo", names(lay))] %||% list(yaxis = list(domain = c(0, 1)))
100101
})
101-
# remove their titles
102-
if (!keep_titles) {
102+
if (!titleX) {
103103
xAxes <- lapply(xAxes, function(ax) lapply(ax, function(y) { y$title <- NULL; y }))
104+
}
105+
if (!titleY) {
104106
yAxes <- lapply(yAxes, function(ax) lapply(ax, function(y) { y$title <- NULL; y }))
105107
}
106108
# number of x/y axes per plot

man/subplot.Rd

Lines changed: 11 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-plotly-subplot.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ test_that("share both axes", {
6868
})
6969

7070
# https://github.com/ropensci/plotly/issues/376
71-
library(plotly)
7271
d <- data.frame(
7372
x = rnorm(100),
7473
y = rnorm(100)

vignettes/subplot.Rmd

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@ knitr::opts_chunk$set(
1818

1919
The `subplot()` function provides a flexible interface for arranging multiple plots in a single view. There are a few different ways to use `subplot()`, but the simplest way to pass plotly visualization objects directly to `subplot()`.
2020

21-
```{r, fig.width = 2}
21+
```{r}
2222
library(plotly)
2323
p1 <- plot_ly(economics, x = date, y = unemploy)
2424
p2 <- plot_ly(economics, x = date, y = uempmed)
2525
subplot(p1, p2)
2626
```
2727

28-
This particular subplot could be improved if we share the x-axis so that zooming and panning events are synchronized. It also makes sense to keep x/y titles and remove the redundant legend (since `subplot()` returns a plotly visualization object, any [layout attribute](https://plot.ly/r/reference/#layout) can be altered via `layout()`).
28+
This particular subplot could be improved if we share the x-axis so that zooming and panning events are synchronized. It also makes sense to keep axis titles and remove the redundant legend (since `subplot()` returns a plotly visualization object, any [layout attribute](https://plot.ly/r/reference/#layout) can be altered via `layout()`).
2929

3030
```{r}
31-
s <- subplot(p1, p2, nrows = 2, shareX = TRUE, keep_titles = TRUE)
31+
s <- subplot(p1, p2, nrows = 2, shareX = TRUE, titleY = TRUE)
3232
layout(s, showlegend = FALSE)
3333
```
3434

@@ -70,13 +70,16 @@ subplot(
7070

7171
The `subplot()` function also understands ggplot2 objects and converts them via `ggplotly()`.
7272

73-
```{r}
73+
```{r, fig.height = 8}
7474
e <- tidyr::gather(economics, variable, value, -date)
7575
gg1 <- ggplot(e, aes(date, value)) + geom_line() +
7676
facet_wrap(~variable, scales = "free_y", ncol = 1)
7777
gg2 <- ggplot(e, aes(factor(1), value)) + geom_violin() +
7878
facet_wrap(~variable, scales = "free_y", ncol = 1) +
79-
theme(axis.text.x = element_blank())
80-
subplot(gg1, gg2)
79+
theme(axis.text = element_blank(), axis.ticks = element_blank())
80+
subplot(gg1, gg2) %>% layout(margin = list(l = 50))
8181
```
8282

83+
84+
85+

0 commit comments

Comments
 (0)