Plotly 的子图功能不适用于 Sankey 图

Plotly's subplot feature not working with Sankey diagram

我正在尝试在散点图旁边绘制桑基图,但由于某种原因 plotly::subplot 无法正确呈现图表,我无法在图表之间获得任何 space。谁能告诉我我做错了什么:

library(plotly)
plot_sankey <- plot_ly(
    type = "sankey",
    orientation = "h",
    node = list(
      label = c("A1", "A2", "B1", "B2", "C1", "C2"),
      color = c("blue", "blue", "blue", "blue", "blue", "blue"),
      pad = 15,
      thickness = 20,
      line = list(
        color = "black",
        width = 0.5
      )
    ),
    link = list(
      source = c(0,1,0,2,3,3),
      target = c(2,3,3,4,4,5),
      value =  c(8,4,2,8,4,2)
    )
  )

plot_scatter <- plot_ly(data = mtcars, x = ~mpg, y = ~hp, type = "scatter", mode = "markers")

subplot(plot_sankey, plot_scatter, nrows = 1)

我有一个有效的方法,但我不确定为什么需要它。 sankey 似乎没有响应我为布局指定为子图元素的任何内容。此方法有效,但您会看到警告。

Warning message:
    layout objects don't have these attributes: 'NA'

在创建 sankey 时指定您想要在子图中使用的域。例如,如果您希望每个地块具有相同数量的 space,并且它们之间的边距为 .1:

library(plotly)

(plot_sankey <- plot_ly(
  type = "sankey",
  domain = list(x = c(0, .45), y = c(0, 1)),  # <-- domain
  orientation = "h",
  node = list(
    label = c("A1", "A2", "B1", "B2", "C1", "C2"),
    color = c("blue", "blue", "blue", "blue", "blue", "blue"),
    pad = 15,
    thickness = 20,
    line = list(
      color = "black",
      width = 0.5
    )
  ),
  link = list(
    source = c(0,1,0,2,3,3),
    target = c(2,3,3,4,4,5),
    value =  c(8,4,2,8,4,2)
  )
))

(plot_scatter <- plot_ly(data = mtcars, x = ~mpg, y = ~hp, 
                         type = "scatter", mode = "markers"))

subplot(plot_sankey, plot_scatter, nrows = 1, margin = .05)

这看起来会偏离中心,因为其中只有一个有图例。如果重要的是它们是偶数,您可以将 showlegend = F 添加到散点图。