Combining Multiple Plots in R Together
Asked Answered
G

3

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?

Guilford answered 24/8, 2022 at 3:50 Comment(2)
You can use manipulateWidget::combineWidgets. See here for an example.Ekg
have you looked into the trelliscope package? If that is an option for you I can generate a specific answerDrida
S
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

Snips answered 24/8, 2022 at 12:37 Comment(4)
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.Snips
@ Kat: thank you for your comment! I found this link here that I am thinking of adapting 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...Guilford
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 crosstalkSnips
I like your idea about Rmarkdown ... I found some examples that I am trying to replicate, e.g. #73492012 Thank you for all your help Kat!Guilford
Y
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

Yoke answered 28/8, 2022 at 9:12 Comment(0)
S
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.

Sniffle answered 24/8, 2022 at 6:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.