R-plotly:子图仅显示底图

R-plotly: Subplot shows only bottom plot

我想在 https://plot.ly/r/subplots/ 中为 mtcars 数据重现 "Scaled Subplots"。

mtcars %>%
  transform(id = as.integer(factor(am))) %>%
  plot_ly(x = ~mpg, y = ~qsec, color = ~factor(vs), yaxis = ~paste0("y", id)) %>%
  add_markers() %>%
  subplot(nrows = 2, shareX = TRUE)

只显示底部的子图:

我以为我忠实地copy/translated 编写了代码,但肯定有问题。 值得注意的是,子图区分 am,而颜色区分 vs。 我为子图和颜色都尝试了 am

mtcars %>%
  transform(id = as.integer(factor(am))) %>%
  plot_ly(x = ~mpg, y = ~qsec, color = ~am, yaxis = ~paste0("y", id)) %>%
  add_markers() %>%
  subplot(nrows = 2, shareX = TRUE)

没有太大帮助,但出现了两个网格: 在后一个示例中,我希望 am==0(蓝色)点位于顶部子图中。

有什么建议吗?

packageVersion('plotly')
[1] ‘4.9.0’

好吧,这里你可以这样做,但必须创建两个图,然后使用 subplot()

p1 = mtcars %>%
      filter(am==0) %>%
      plot_ly(x = ~mpg, y = ~qsec, color = ~vs, legendgroup = "am0", name = "0",
          type = "scatter" , mode = "markers", showlegend = FALSE)

p2 = mtcars %>%
      filter(am==1) %>%
      plot_ly(x = ~mpg, y = ~qsec, color = ~vs, legendgroup = "am1", name = "1",
          type = "scatter" , mode = "markers")

subplot(p1,p2,nrows = 2, shareX = TRUE, titleY = TRUE, which_layout = 1)

此处输出: