在R语言中将多个图形合并在一起

8

使用R语言中的"plotly"库,我生成了一些随机数据并制作了交互式数据可视化:

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)

接着,我使用“plotly”库中的“subplot”函数制作了以下三个子图:

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)

我想知道是否有可能将这三个子图组合成一个单独的“对象”(可以后续保存为HTML文件,例如使用htmlwidgets),其外观类似于此:
#pseudocode (e.g. imagine some "wrap" function)
results = wrap(subplot1, subplot2, subplot3)
 saveWidget(   results,   "results.html")

enter image description here

也就是说,将这三个子图组合在一起,让用户可以在它们之间导航。这是可能的吗?

您可以使用 manipulateWidget::combineWidgets。请参见此处的示例。 - Stéphane Laurent
你有研究过Trelliscope包吗?如果这是一个选项,我可以生成一个具体的答案。 - DPH
3个回答

5

有许多种方法可以组合这些图表。最简单的方法可能是再次使用 subplot

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

enter image description here


谢谢Kat!我会继续研究看看是否有其他的方法来完成这个任务! - stats_noob
你可以随时使用 htmltools::browsable。例如,browsable(div(subplot1, subplot2, subplot2))。然而,你可能需要添加样式,因为它使用默认的宽度和高度,这个宽度是100%,高度是400像素,原因我无法理解。所以有时候会变得非常丑陋。 - Kat
@ Kat:谢谢你的留言!我在这里找到了一个链接,正在考虑进行调整https://plotly.com/r/dropdowns/ ...但现在我不知道如何操作...我打算明天在这个问题上添加一份赏金,看看是否会引起更多的关注... - stats_noob
使用Plotly下拉菜单,可以为一个图形上的曲线添加下拉菜单。如果您想要创建一个下拉菜单来在不同图形之间进行选择,则必须使用JS、Shiny、RMD或潜在的crosstalk - Kat
我喜欢你关于Rmarkdown的想法...我找到了一些示例,正在尝试复制,例如https://dev59.com/LVEG5IYBdhLWcg3wH0jT。谢谢你的帮助,Kat! - stats_noob

3
一个选择是在Rmarkdown html中创建一个下拉菜单,使用{.tabset .tabset-dropdown}来显示每个子图。以下是可重现的例子:
---
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
```

输出:

在这里输入图像描述


2

听起来你想要导出响应式元素,这似乎不可导出。我知道shiny可以模块化以便重复使用,但我认为这不能导出到htmlwidget。如果图表1、3、5和2、4、6是相同类型的,并且您制作了自定义滑块,则可能是可能的。对我来说听起来很麻烦。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接