使用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")
也就是说,将这三个子图组合在一起,让用户可以在它们之间导航。这是可能的吗?
manipulateWidget::combineWidgets
。请参见此处的示例。 - Stéphane Laurent