Plotly scaleY 不适用于子图行

Plotly scaleY not working across subplot rows

基于另一个问题 (),我遇到了一个新问题。我希望两行中的所有图都具有相同的 Y 轴。但是,如果我转 "shareY = TRUE",上排的图共享一个轴,下排的图共享一个轴,但轴彼此不同。

代码基本上是@Joris Chau 的答案中的代码,但在最后一行添加了 "shareY = TRUE"。

library(plotly)
library(tidyverse)

mpg %>%
  mutate_at("trans", as.factor) %>%  
  group_by(class) %>%
  group_map(.f = ~{          
    ## fill missing levels w/ displ = 0, cyl = first available value 
    complete(.x, trans, fill = list(displ = 0, cyl = head(.x$cyl, 1))) %>%
      plot_ly(x = ~cyl, y = ~displ, color = ~trans, colors = "Paired", type = "bar",
              showlegend = (.y == "2seater"), legendgroup = ~trans) %>%
      layout(yaxis = list(title = as.character(.y)), barmode = "stack")
  }) %>%
  subplot(nrows = 2, shareX = TRUE, shareY = TRUE, titleY = TRUE)   

我如何告诉 plotly 在所有地块上使用相同的比例?

您应该手动定义 yaxis 的范围。在这里,我使用了c(0,ceiling(max(aggregate(displ ~ cyl+class, mpg, sum)$displ)/10)*10))

aggregate(displ ~ cyl+class, mpg, sum)$displ 得到按 cyl + class 分组的 displ 的总和。

然后我得到它的最大值,最后我用 ceiling.

将它四舍五入
library(plotly)
library(tidyverse)

mpg %>%
  mutate_at("trans", as.factor) %>%  
  group_by(class) %>%
  group_map(.f = ~{          
   complete(.x, trans, fill = list(displ = 0, cyl = head(.x$cyl, 1))) %>%
    plot_ly(x = ~cyl, y = ~displ, color = ~trans, colors = "Paired", type = "bar",
            showlegend = (.y == "2seater"), legendgroup = ~trans) %>%
     layout(yaxis = list(title = as.character(.y), 
                         range=c(0, ceiling(max(
                                     aggregate(displ~cyl+class, mpg, sum)$displ)/10)*10)),
            barmode = "stack")
  }) %>%
  subplot(nrows = 2, shareX = TRUE, shareY = FALSE, titleY = TRUE, margin = 0.05)