You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在R中如何将多个dygraphs::dygraph()绘图按指定行列布局排列

在R中用htmltools排列dygraphs的多列多行布局

1列6行布局

你已经实现的1列6行布局可以直接沿用,也可以通过htmltools::div明确包裹来保持结构清晰,代码示例如下:

library(dygraphs)
library(htmltools)

# 示例时间序列数据
mdeaths <- structure(c(2134, 1863, 1877, 1877, 1492, 1249, 1280, 1131, 1209, 1492, 1621, 1846, 2103, 2137, 2153, 1833, 1403, 1288, 1186, 1133, 1053, 1347, 1545, 2066, 2020, 2750, 2283, 1479, 1189, 1160, 1113, 970, 999, 1208, 1467, 2059, 2240, 1634, 1722, 1801, 1246, 1162, 1087, 1013, 959, 1179, 1229, 1655, 2019, 2284, 1942, 1423, 1340, 1187, 1098, 1004, 970, 1140, 1110, 1812, 2263, 1820, 1846, 1531, 1215, 1075, 1056, 975, 940, 1081, 1294, 1341), tsp = c(1974, 1979.91666666667, 12), class = "ts")

# 生成6个带标题的dygraph对象列表
Plot_List <- lapply(1:6, function(x) {
  dygraph(mdeaths) %>% 
    dyOptions(title = paste("图表", x))
})

# 1列6行布局:tagList默认垂直排列所有绘图
browsable(tagList(Plot_List))

# 或者用div包裹,结构更明确
browsable(div(Plot_List))

2列3行布局

要实现2列3行的布局,利用htmltools内置的Bootstrap栅格系统,将每个绘图放在col-md-6类的容器(中等及以上屏幕占半宽),每两个绘图组成一行(row类容器):

# 将绘图列表按每2个一组拆分
plot_groups <- split(Plot_List, ceiling(seq_along(Plot_List)/2))

# 构建每行布局:每行包含两个半宽的绘图容器
layout_rows <- lapply(plot_groups, function(group) {
  div(class = "row",
      div(class = "col-md-6", group[[1]]),
      div(class = "col-md-6", group[[2]])
  )
})

# 渲染最终布局
browsable(tagList(layout_rows))

如果需要适配小屏幕(自动转为1列),Bootstrap栅格类已默认支持,无需额外配置。你也可以将col-md-6替换为col-sm-6或col-lg-6,调整不同屏幕尺寸下的列宽规则。

内容的提问来源于stack exchange,提问作者David Moore

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 07:55:37