8

Using the "plotly" library in R - I generated some random data and made some interactive data visualizations:

library(plotly)
library(ggplot2)
library(dplyr)
library(hrbrthemes)

#subplot 1

data1 <- data.frame(
  day = as.Date("2017-06-14") - 0:364,
  value = runif(365) - seq(-140, 224)^2 / 10000
)

p1 <- ggplot(data1, aes(x=day, y=value)) +
  geom_line( color="#69b3a2") + 
  xlab("") +
  theme_ipsum() +
  theme(axis.text.x=element_text(angle=60, hjust=1)) 

fig1 <- ggplotly(p1)

scatter_1 = data.frame(x = rnorm(100,100,100), y = rnorm(100,100,100))

fig2 <- plot_ly(data = scatter_1, x = ~x, y = ~y)



#subplot 2

data2 <- data.frame(
  day = as.Date("2017-06-14") - 0:364,
  value = runif(365) - seq(-140, 224)^2 / 10000
)

p2 <- ggplot(data2, aes(x=day, y=value)) +
  geom_line( color="#69b3a2") + 
  xlab("") +
  theme_ipsum() +
  theme(axis.text.x=element_text(angle=60, hjust=1)) 

fig3 <- ggplotly(p2)

scatter_2 = data.frame(x = rnorm(100,100,100), y = rnorm(100,100,100))

fig4 <- plot_ly(data = scatter_1, x = ~x, y = ~y)

#subplot 3

data3 <- data.frame(
  day = as.Date("2017-06-14") - 0:364,
  value = runif(365) - seq(-140, 224)^2 / 10000
)

p3 <- ggplot(data3, aes(x=day, y=value)) +
  geom_line( color="#69b3a2") + 
  xlab("") +
  theme_ipsum() +
  theme(axis.text.x=element_text(angle=60, hjust=1)) 

fig5 <- ggplotly(p3)

scatter_3 = data.frame(x = rnorm(100,100,100), y = rnorm(100,100,100))

fig6 <- plot_ly(data = scatter_3, x = ~x, y = ~y)

After this, I used the "subplot" function in the "plotly" library to make the three following subplots:

subplot1 <- subplot(fig1, fig2,  nrows = 1, margin = 0.05)
subplot2 <- subplot(fig3, fig4, nrows = 2, margin = 0.05)
subplot3 <- subplot(fig5, fig6, nrows = 2, margin = 0.05)

I was wondering if its possible to combine these three subplots into a single "object" (that can be later saved as an HTML file, e.g. using htmlwidgets) that would look something like this:

#pseudocode (e.g. imagine some "wrap" function)
results = wrap(subplot1, subplot2, subplot3)
 saveWidget(   results,   "results.html")

enter image description here

That is, combine the plots in such a way that the user can navigate between these 3 subplots. Is this possible?

stats_noob
  • 5,401
  • 4
  • 27
  • 83
  • You can use `manipulateWidget::combineWidgets`. See [here](https://stackoverflow.com/a/72855385/1100107) for an example. – Stéphane Laurent Sep 01 '22 at 13:01
  • have you looked into the [trelliscope](https://cran.r-project.org/web/packages/trelliscopejs/vignettes/trelliscopejs.html) package? If that is an option for you I can generate a specific answer – DPH Sep 02 '22 at 17:59

3 Answers3

5

There are many ways to combine these. The easiest way is probably using subplot again.

subplot(subplot1, subplot2, subplot3, nrows = 3, margin = .05, 
        heights = c(.2, .4, .4)) # proportion of subplot height

enter image description here

Kat
  • 15,669
  • 3
  • 18
  • 51
  • Thank you Kat! I will keep looking into this and see if there is another way to accomplish this as well! – stats_noob Aug 24 '22 at 17:56
  • You can always use `htmltools::browsable`. For example, `browsable(div(subplot1, subplot2, subplot2))` However, you're going to want to add styles, because it uses the default width and height, which for some reason I can't fathom is width = 100% and height = 400px. So, it can be a hot ugly mess sometimes. – Kat Aug 24 '22 at 18:45
  • @ Kat: thank you for your comment! I found this link here that I am thinking of adapting https://plotly.com/r/dropdowns/ ... but right now I don't know how to do this ...I am thinking of adding a bounty to this question tomorrow and seeing if it will attract some more attention... – stats_noob Aug 24 '22 at 23:59
  • With Plotly dropdowns, that's for the traces on one plot. If you want to create a dropdown to select between plots, you have to use JS, Shiny, RMD, or potentially `crosstalk` – Kat Aug 25 '22 at 03:27
  • I like your idea about Rmarkdown ... I found some examples that I am trying to replicate, e.g. https://stackoverflow.com/questions/73491512/creating-dropdown-menus-in-r Thank you for all your help Kat! – stats_noob Aug 25 '22 at 17:26
3

An option could be creating a dropdown menu in Rmarkdown html to show each subplot with in a dropdown menu using {.tabset .tabset-dropdown}. Here is a reproducible example:

---
title: "Combining Multiple Plots in R Together"
date: "2022-08-28"
output: html_document
---

# Subplots {.tabset .tabset-dropdown}

```{r, warning=FALSE, echo=FALSE, message=FALSE}
library(plotly)
library(ggplot2)
library(dplyr)
library(hrbrthemes)

#subplot 1

data1 <- data.frame(
  day = as.Date("2017-06-14") - 0:364,
  value = runif(365) - seq(-140, 224)^2 / 10000
)

p1 <- ggplot(data1, aes(x=day, y=value)) +
  geom_line( color="#69b3a2") + 
  xlab("") +
  theme_ipsum() +
  theme(axis.text.x=element_text(angle=60, hjust=1)) 

fig1 <- ggplotly(p1)

scatter_1 = data.frame(x = rnorm(100,100,100), y = rnorm(100,100,100))

fig2 <- plot_ly(data = scatter_1, x = ~x, y = ~y)



#subplot 2

data2 <- data.frame(
  day = as.Date("2017-06-14") - 0:364,
  value = runif(365) - seq(-140, 224)^2 / 10000
)

p2 <- ggplot(data2, aes(x=day, y=value)) +
  geom_line( color="#69b3a2") + 
  xlab("") +
  theme_ipsum() +
  theme(axis.text.x=element_text(angle=60, hjust=1)) 

fig3 <- ggplotly(p2)

scatter_2 = data.frame(x = rnorm(100,100,100), y = rnorm(100,100,100))

fig4 <- plot_ly(data = scatter_1, x = ~x, y = ~y)

#subplot 3

data3 <- data.frame(
  day = as.Date("2017-06-14") - 0:364,
  value = runif(365) - seq(-140, 224)^2 / 10000
)

p3 <- ggplot(data3, aes(x=day, y=value)) +
  geom_line( color="#69b3a2") + 
  xlab("") +
  theme_ipsum() +
  theme(axis.text.x=element_text(angle=60, hjust=1)) 

fig5 <- ggplotly(p3)

scatter_3 = data.frame(x = rnorm(100,100,100), y = rnorm(100,100,100))

fig6 <- plot_ly(data = scatter_3, x = ~x, y = ~y)

subplot1 <- subplot(fig1, fig2,  nrows = 1, margin = 0.05)
subplot2 <- subplot(fig3, fig4, nrows = 2, margin = 0.05)
subplot3 <- subplot(fig5, fig6, nrows = 2, margin = 0.05)
```

## subplot 1

```{r, echo=FALSE}
subplot1
```

## subplot 2

```{r, echo=FALSE}
subplot2
```

## subplot 3 

```{r, echo=FALSE}
subplot3
```

Output:

enter image description here

Quinten
  • 35,235
  • 5
  • 20
  • 53
2

Sounds like you want to export reactive elements, which doesn't sound exportable. I know shiny can be modularized to be reusable, but I don't think that's exportable to htmlwidget. It may be possible if plots 1, 3, 5, and 2,4,6 are the same type and you make a custom slider. Sounds like a pain to me.

R_Pseudo
  • 146
  • 5