Skip to content

Commit 79650d7

Browse files
committed
better defaults; start on a vignette
1 parent 961e73d commit 79650d7

File tree

4 files changed

+92
-6
lines changed

4 files changed

+92
-6
lines changed

R/subplots.R

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ subplot <- function(..., nrows = 1, widths = NULL, heights = NULL, shareX = FALS
5757
plots <- list()
5858
for (i in seq_along(plotz)) {
5959
p <- plots[[i]] <- plotz[[i]]
60-
layoutAttrs <- names(p$layout)
60+
layoutAttrs <- c(names(p$layout), c("geo", "xaxis", "yaxis"))
6161
xTraceAttrs <- sub("^x", "xaxis", sapply(p$data, function(tr) tr[["geo"]] %||% tr[["xaxis"]]))
6262
yTraceAttrs <- sub("^y", "yaxis", sapply(p$data, function(tr) tr[["geo"]] %||% tr[["yaxis"]]))
6363
missingAttrs <- setdiff(c(xTraceAttrs, yTraceAttrs), layoutAttrs)
@@ -92,8 +92,12 @@ subplot <- function(..., nrows = 1, widths = NULL, heights = NULL, shareX = FALS
9292
x$annotations[!axes]
9393
})
9494
# collect axis objects
95-
xAxes <- lapply(layouts, function(lay) lay[grepl("^xaxis|^geo", names(lay))])
96-
yAxes <- lapply(layouts, function(lay) lay[grepl("^yaxis|^geo", names(lay))])
95+
xAxes <- lapply(layouts, function(lay) {
96+
lay[grepl("^xaxis|^geo", names(lay))] %||% list(xaxis = list(domain = c(0, 1)))
97+
})
98+
yAxes <- lapply(layouts, function(lay) {
99+
lay[grepl("^yaxis|^geo", names(lay))] %||% list(yaxis = list(domain = c(0, 1)))
100+
})
97101
# remove their titles
98102
if (!keep_titles) {
99103
xAxes <- lapply(xAxes, function(ax) lapply(ax, function(y) { y$title <- NULL; y }))

tests/testthat/test-plotly-subplot.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ s <- subplot(
8484
)
8585

8686
test_that("Row/column height/width", {
87-
l <- expect_traces(s, 4, "width-height")
87+
l <- expect_traces(s, 3, "width-height")
8888
expect_equal(diff(l$layout$xaxis$domain), 0.8 - 0.005)
8989
expect_equal(diff(l$layout$xaxis2$domain), 0.2 - 0.005)
9090
expect_equal(diff(l$layout$yaxis$domain), 0.2 - 0.005)

vignettes/intro.Rmd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ You can manually add a trace to an existing plot with `add_trace()`. In that cas
3636
```{r}
3737
m <- loess(unemploy / pop ~ as.numeric(date), data = economics)
3838
p <- plot_ly(economics, x = date, y = unemploy / pop, name = "raw")
39-
add_trace(p, y = fitted(m), name = "loess")
39+
add_trace(p, x = date, y = fitted(m), name = "loess")
4040
```
4141

4242
__plotly__ was designed with a [pure, predictable, and pipeable interface](https://dl.dropboxusercontent.com/u/41902/pipe-dsls.pdf) in mind, so you can also use the `%>%` operator to create a visualization pipeline:
4343

4444
```{r}
4545
economics %>%
4646
plot_ly(x = date, y = unemploy / pop) %>%
47-
add_trace(y = fitted(m)) %>%
47+
add_trace(x = date, y = fitted(m)) %>%
4848
layout(showlegend = F)
4949
```
5050

vignettes/subplot.Rmd

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
---
2+
title: "The `subplot()` function"
3+
author: "Carson Sievert"
4+
output: rmarkdown::html_vignette
5+
vignette: >
6+
%\VignetteEngine{knitr::rmarkdown}
7+
%\VignetteIndexEntry{subplot}
8+
---
9+
10+
```{r, echo = FALSE}
11+
knitr::opts_chunk$set(
12+
message = FALSE,
13+
warning = FALSE,
14+
fig.width = 4,
15+
fig.height = 2
16+
)
17+
```
18+
19+
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()`.
20+
21+
```{r, fig.width = 2}
22+
library(plotly)
23+
p1 <- plot_ly(economics, x = date, y = unemploy)
24+
p2 <- plot_ly(economics, x = date, y = uempmed)
25+
subplot(p1, p2)
26+
```
27+
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()`).
29+
30+
```{r}
31+
s <- subplot(p1, p2, nrows = 2, shareX = TRUE, keep_titles = TRUE)
32+
layout(s, showlegend = FALSE)
33+
```
34+
35+
By default, every subplot is allocated equal spacing, and the margins between them are set somewhat arbitrarily, but that can easily be altered:
36+
37+
```{r}
38+
map <- plot_ly(
39+
z = state.area, text = state.name, locations = state.abb,
40+
type = 'choropleth', locationmode = 'USA-states', geo = "geo"
41+
)
42+
# specify some map projection/options
43+
g <- list(
44+
scope = 'usa',
45+
projection = list(type = 'albers usa'),
46+
lakecolor = toRGB('white')
47+
)
48+
map <- layout(map, geo = g)
49+
subplot(
50+
plot_ly(x = state.name, y = state.area, type = "bar"), map,
51+
nrows = 2, margin = c(0, 0, 0.25, 0), heights = c(0.2, 0.8)
52+
)
53+
```
54+
55+
In addition to a `heights` argument for controlling the proportional height of each row, there is also a `widths` argument for controlling the proportional width of each column:
56+
57+
```{r}
58+
x <- rnorm(100)
59+
y <- rnorm(100)
60+
m <- list(color = "black")
61+
subplot(
62+
plot_ly(x = x, type = "histogram", marker = m),
63+
plotly_empty(),
64+
plot_ly(x = x, y = y, mode = "markers", marker = m),
65+
plot_ly(y = y, type = "histogram", marker = m),
66+
nrows = 2, heights = c(0.2, 0.8), widths = c(0.8, 0.2),
67+
shareX = TRUE, shareY = TRUE
68+
) %>% layout(showlegend = FALSE)
69+
```
70+
71+
The `subplot()` function also understands ggplot2 objects and converts them via `ggplotly()`.
72+
73+
```{r}
74+
e <- tidyr::gather(economics, variable, value, -date)
75+
gg1 <- ggplot(e, aes(date, value)) + geom_line() +
76+
facet_wrap(~variable, scales = "free_y", ncol = 1)
77+
gg2 <- ggplot(e, aes(factor(1), value)) + geom_violin() +
78+
facet_wrap(~variable, scales = "free_y", ncol = 1) +
79+
theme(axis.text.x = element_blank())
80+
subplot(gg1, gg2)
81+
```
82+

0 commit comments

Comments
 (0)