|
| 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